bijan
bijan

Reputation: 1685

RESTful backend and socket.io to sync

Today, i had the idea of the following setup. Create a nodejs server along with express and socket.io. With express, i would create a RESTful API, which is connected to a mongo. BackboneJS or similar would connect the client to that REST API.
Now every time the mongodb(ie the data in it iam interested in) changes, socket.io would fire an event to the client, which would carry a courser to the data which has changed. The client then would trigger the appropriate AJAX requests to the REST to get the new data, where it needs it.

So, the socket.io connection would behave like a synchronize trigger. It would be there for the entire visit and could also manage sessions that way. All the payload would be send over http.

Pros:

Cons:

What do you think, are there any great disadvantages i did not think of?

Upvotes: 9

Views: 6846

Answers (3)

Octavian Helm
Octavian Helm

Reputation: 39604

I'd look into Now.js since it does pretty much exactly what you need.

It creates a namespace which is shared among the client and server. The server can call functions on the client directly and vice versa.

That is if you insist on your current infrastructure decision to use MongoDB and Node.js, otherwise there would be CouchDB which is a full web server and document database with sophisticated replication mechanisms built-in.

Upvotes: 1

Lance Pollard
Lance Pollard

Reputation: 79188

I agree with @CharlieKey, you should send the updated data rather than re-requesting.

This is exactly what Tower is doing:

The disadvantage of using sockets as a trigger to re-request with Ajax is that every connected client will have to fetch the data, so if 100 people are on your site there's going to be 100 HTTP requests every time data changes - where you could just reuse the socket connections.

Upvotes: 6

Charlie Key
Charlie Key

Reputation: 3440

I think that pushing the updated data with the socket.io event would be better than re-requesting the lastest. Even better you could only push the modified pieces of data decreasing the amount of data sent over the line. Overall though a interesting idea.

Upvotes: 3

Related Questions