Answer the question
In order to leave comments, you need to log in
How to implement communication with the server on websockets?
Good afternoon!
I am currently doing SPA on Backbone. For realtime I use web sockets (SockJS). On the node.js server
Actually, one question arose: how best to communicate with the client application with the server - through websockets or rest api? For example, I send a message to a chat. This can be done as follows:
1. We send a message immediately to websockets and on the server, in the onmessage event handler, save the message to the database and send it to all recipients.
2. We send a message to the server via the HTTP REST API, save it to the database and send it to all recipients via websockets.
Which approach is more correct? And what are their downsides? Thank you.
Answer the question
In order to leave comments, you need to log in
If you do not need access from native clients, then I think websockets are the best solution. The fact is that http itself and various crutches like polling , long polling , in principle, are not intended for real-time:
1) overhead for each request;
2) timeouts and constant reconnects to check for new messages;
3) the more messages per unit of time, the higher the chance of not receiving it (during reconnect);
4) when new data appears, the server cannot send them on its own - only in response to a request.
Websockets are free of these shortcomings, and are great for realtime.
If neededREST API for native clients, then make a small backend based on http : it's easier to send an http request than to pervert with various c++ websocket implementations (as an example).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question