3

I have been using several apps including StackExchange Chat (https://chat.stackoverflow.com/).

Let say I open https://chat.stackoverflow.com/ and start chatting, after few messages, I change my computer's IP, but after that, I am still able to receive messages.

How does the server know that a particular client was connected to it and it sent message?

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
  • 2
    If you open a stack overflow chat session and look at it in the debugger, you can see that new messages arrive on a webSocket connection. So, presumably, when your IP address changes, the webSocket connection gets broken and the client code in the chat page realizes that and re-establishes the connection from your new IP. – jfriend00 Mar 04 '18 at 17:14
  • Yup, just like any other kind of IP-based protocol. The connection endpoints are identified by IPs. If an IP changes, the connection breaks and must be recreated – Remy Lebeau Mar 05 '18 at 20:13

1 Answers1

1

My guess is that the client detects the websocket has died, and recreates it. This goes through the whole creation process again, so it doesn't matter if it died because the IP changed or something else happened, like temporary loss of connection

When the server detects a websocket has died, it simply drops it. The client does all the work

The server just knows it has a number of websockets belonging to a specific user, and will send on all of them

Suppen
  • 836
  • 1
  • 6
  • 21
  • So, `webSockets` don't have auto-reconnect logic built in. That type of logic has to be built on top of webSockets. The socket.io library which is built on top of webSockets does iimplement auto-reconnect for you. It also uses a regular heartbeat message to better detect when the existing connection has become inoperative. – jfriend00 Mar 04 '18 at 17:09