Answer the question
In order to leave comments, you need to log in
How to synchronize WS and ajax correctly?
I have ajax and ws clients but chat 1. I'm thinking of doing something like this.
When any user enters the chat, a request is made to the ajax version to receive messages from the database. If the user supports ws then we connect to the server. When sending a ws message, the server sends it to all connected users and adds it to the database. When sending an ajax message to the server, it sends a message to the socket and waits for a response (so that the message is definitely added to the database), as soon as the socket answered, it pulls n messages from the database and gives them to the user. The ajax chat is not updated automatically (the user must press "refresh").
Answer the question
In order to leave comments, you need to log in
On one project I did it simply - the first request loads the necessary messages from the database, and the client renders them. If the interlocutor sends a message, then first there is an Ajax insertion, after that, messages are sent via a web socket. On the backend, a node is responsible for delivery via a websocket, you can solder the demon on anything you like. Its task is only to send a message to the desired client in the socket and that's it. It turns out that the message will be in the database and if the interlocutor has the opportunity, he will receive everything he needs in real time
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question