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.
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.
Share Improve this question edited Sep 26, 2014 at 12:17 Tom Doyle 1823 silver badges15 bronze badges asked Jun 28, 2013 at 7:53 Drew KhouryDrew Khoury 1,3908 silver badges21 bronze badges 4- Isn't it as simple as the server sends the list of currently connected clients to any new client? – JeffRSon Commented Jun 28, 2013 at 7:57
- @JeffRSon I want to be able to send any arbitrary piece of data. – Drew Khoury Commented Jun 28, 2013 at 8:06
- I'm not sure whether it makes sense to send outdated messages. Let's assume a client had connected and disconnected. Will you send both messages to any new client? Or will you know, that a disconnected message has to "delete" a previous connected message? If it's not for logging you should only send current state. And for logging you have to keep the whole bunch of messages in order to send it - however only once of course. – JeffRSon Commented Jun 28, 2013 at 8:32
- would this be possible here: stackoverflow./questions/26056688/… – Tom Doyle Commented Sep 26, 2014 at 11:37
2 Answers
Reset to default 8 +50You could do something like this:
- Each message should have an id -> muid (Message Unique ID)
- Each time a client send s a message, it gets an ACK from the server along with the muid for the sent message.
- 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.
- 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:
- Full history: I would remend a database storage with proper indexing
- 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
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./nodejs-and-websocket-simple-chat-tutorial