To solve your problem, messages for the room need to be saved somewhere, and then resent to individual clients as necessary.
The most obvious place to store messages is the server side of the data warehouse (e.g. Redis). Keep each conversation effective as a list of events, adding new events as they occur.
A simple scheme works as follows:
- Each broadcast message has a UUID attached to it. When the server processes a new message, it adds the message to the list for this "room".
- When a client connects / reconnects, it sends a message (for example, "LAST_MESSAGE_RECEIVED") indicating the UUID of the last message it received.
- When the server receives one of these LAST_MESSAGE_RECEIVED messages, it checks if this is the last message for the room, and if not, it sends a message only to this separate socket with an array of missed messages. The client is now updated.
Alternative: if you do not need to save history after the end of the session, you can be smart and take advantage of the fact that other clients already store messages and ask clients to resend messages on a peer-to-peer network, in a flat way. This avoids the need to have your own server-side data storage.
decates Apr 20 '17 at 15:50 2017-04-20 15:50
source share