SocketCluster / socketcluster

Highly scalable realtime pub/sub and RPC framework
https://socketcluster.io
MIT License
6.14k stars 313 forks source link

any way to send the missing socket update to client after network restore #585

Closed UsmanShafaqat007 closed 1 year ago

UsmanShafaqat007 commented 1 year ago

due to the network being down the socket got disconnected at the client's end and the client missed the update during that time. is there any way to send that updates again when the socket connection is restored?

maarteNNNN commented 1 year ago

You could do that, but for that you would have to track clients yourself. SocketCluster itself provides socket.id's but these will change on reconnects. I tested this the changing of the ids via the a client:

const socket = sccClient.create({
  hostname: 'localhost',
  port: 8000,
  autoConnect: false,
});

(async () => {
  socket.connect();

  for await (const event of socket.listener('connect')) {
    console.log('connected', socket.id);

    socket.disconnect();

    setTimeout(() => {
      console.log('reconnecting');
      socket.reconnect();
    }, 1000);
  }

  for await (const event of socket.listener('disconnect')) {
    console.log('disconnected', socket.id);
  }
})()

outputs:

connected ba2gYsb9F-gzcQ-EAAAH
reconnecting
connected aYns1ySMFa41s-MFAAAI
reconnecting
connected EtUcSTujH4pNWZKKAAAJ

What could work is that you set a uuid on the client, send it to the server and if it disconnects store the payload somewhere and send it again once the same uuid connects again. Although this probably require some convoluted code to achieve that. I'm unclear if there is something built in to SC @jondubois for this?

UsmanShafaqat007 commented 1 year ago

you are talking about the connection and disconnection update i am talking about the update that clients missed during their socket disconnection. e.g the socket is down due to the network and there are some messages for the client during that time how can we send that messages again when the client socket is connected.

maarteNNNN commented 1 year ago

Let me rephrase, you can't rely on socket.id. You will have to setup ids of your own on your clients, on connection you will pass them on to the server and then you can track what's their state (connected or disconnected). I don't think this will be easy to implement.

I suggest that you instead store the messages on the server and use .invoke('latest') on the client and setup a route .procedure('latest') that provides the latest updates.

Make sense?