Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send old messages with Websockets

I've got a working Websockets example, where clients receive messages from the server.

I'm not sure how I should send old messages to clients when they connect.

Example:

  • Each client supplies their name when they connect
  • The server responds with "[name] just connected" (to all clients)
  • Any new clients would NOT get these messages

I'm wondering if there's any way clients can receive old messages (either all of them, or messages in the last 5 minutes would be acceptable).

I suspect I may have to capture this information myself, store it somewhere (like a database) and send the messages to new clients myself. Is that right, or am I missing something?

If anyone has pseudo code, or a link to an example of how others have implemented this, that would be handy.

like image 433
Drew Khoury Avatar asked Jun 28 '13 07:06

Drew Khoury


People also ask

How do I send a message to a WebSocket?

To send a message through the WebSocket connection you call the send() method on your WebSocket instance; passing in the data you want to transfer. socket. send(data); You can send both text and binary data through a WebSocket.

Can WebSocket lose messages?

Websocket client connections may drop due to intermittent network issue and when connections drop, messages will also be lost.

How long can WebSockets last?

A WebSocket times out if no read or write activity occurs and no Ping messages are received within the configured timeout period. The container enforces a 30-second timeout period as the default.

Do WebSockets reconnect automatically?

How reconnections occur. With the standard WebSocket API, the events you receive from the WebSocket instance are typically: onopen onmessage onmessage onmessage onclose // At this point the WebSocket instance is dead. This is all handled automatically for you by the library.


2 Answers

You could do something like this:

  1. Each message should have an id -> muid (Message Unique ID)
  2. Each time a client send s a message, it gets an ACK from the server along with the muid for the sent message.
  3. Each time a new message is received in the server side, a muid is assigned, sent with the ACK and also sent with the message to every connected user. This way the view will be able to present, for every user, the same sequence at some point in the time.
  4. Each time a new user connects it sends the last muid it has received so the server knows where this user stopped receiving messages. The server could then send as many old messages as you want, depending on the kind of storage you implement:
    1. Full history: I would recommend a database storage with proper indexing
    2. Last N messages: Depending on the size of N you could simply store the last N messages in a fixed size Array and send them, all or the needed chunk, on each reconnection. Keep in mind that this will consume memory so, storing last 1024 messages for 1024 different chats would eat quite a bit of memory, specially if messages are of unlimited size.

Hope it helps

like image 166
Leo Nomdedeu Avatar answered Oct 15 '22 06:10

Leo Nomdedeu


You will have to capture it by your own and store it on server... once user connects you will have to name that data to all connected clients and the messages which you have stored back to the user who has connected. So, you will have to code to broadcast the data to users

By the way what are you using server side? (Node, Erlang , etc)

You can check following link if you are using node.js

http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial

like image 43
Jack Daniel's Avatar answered Oct 15 '22 05:10

Jack Daniel's