RESTful backend and socket.io for synchronization

Today I had the idea of ​​the following setup. Create a nodejs server along with express and socket.io . Using an expression, I create a RESTful API that connects to mongo. BackboneJS or similar will connect the client to this REST API.
Now every time mongodb changes (i.e., data is interested in it), socket.io fires an event for the client, which moves the cursor to the changed data. The client then runs the appropriate AJAX requests in REST to retrieve new data where necessary.

So socket.io connection will behave like a synchronization trigger. He will be present throughout the visit and can also manage sessions. All payload will be sent via http.

Pros:

  • REST API for use with clients other than web
  • Auth can be done completely through socket.io. Only send token along with REST requests.
  • Take advantage of REST.
  • Would also play well with a pub / support service like Redis'

Minuses:

  • Greater overhead than using pure socket.io.

Do you think there are any big flaws that I did not think about?

+6
source share
3 answers

I agree with @CharlieKey, you should send updated data, not re-request.

This is exactly what the Tower does:

The disadvantage of using sockets as a trigger for a second request with Ajax is that each connected client will have to retrieve data, so if there are 100 people on your site, there will be 100 HTTP requests each time you change the data. you can just reuse socket connections.

+5
source

I think that pushing the updated data with the socket.io event would be better than re-querying the latter. Even better, you could only click the modified pieces of data, reducing the amount of data sent along the line. All in all, although an interesting idea.

+2
source

I would take a look at Now.js , since you pretty much definitely need it.

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

That is, if you insist on using your infrastructure solution with MongoDB and Node.js, otherwise you will have CouchDB , which is a complete web server and document database with built-in replication mechanisms.

+1
source

Source: https://habr.com/ru/post/920749/


All Articles