Can websocket messages get lost or not?

11,026

Solution 1

It can happen. TCP guarantees the order of packets, but it does not mean that all packets sent from a server reach a client even when an unrecoverable trouble happens in an underlying network. Imagine someone pulls out your LAN cable or switches off your WiFi access point at the worst timing while your application is communicating with your server. TCP does not overcome such a trouble.

To ensure that every WebSocket message sent from your server reaches your client, you have to implement some kind of SYN/ACK in the application layer.

Solution 2

TCP is a guaranteed protocol - packets will be received in the correct order by the higher application levels at the far end (this is as opposed to UDP which is a send and hope protocol).

Generally speaking TCP should be be used for connections where all the data must arrive correctly at the far end. UDP is used where a missing packet can be dropped without significant issue (e.g. streaming services, NTP updates)

Solution 3

In my game, to counter missed web socket messages, I added an int/long ID for each message. When the client detects that something is wrong in the sequence of IDs it receives, the client will request for new data from the server to be able to recover properly.

Share:
11,026

Related videos on Youtube

Adrian Krebs
Author by

Adrian Krebs

Software Engineer from Bern, Switzerland, with a passion for Software Development. Enthusiastic about new technologies, design and innovation. Always eager to learn.

Updated on September 15, 2022

Comments

  • Adrian Krebs
    Adrian Krebs over 1 year

    I'm currently developing a Java WebSocket Client Application and I have to make sure that every message from the server is received by the client. Is it possible that I lose some messages (once they are sent from the server) due to a connection interruption? WebSocket is based on TCP so this shouldn't happen right?

    • symbiont
      symbiont almost 5 years
      received does not mean it was read
  • symbiont
    symbiont almost 5 years
    it looks like you are suggesting that TCP packets that don't arrive can go unnoticed. isn't the problem in the application layer, where the hardware has already sent an ACK but the application crashes before reading the received message?
  • Jayen
    Jayen over 4 years
    are you thinking of web rtc? web sockets should be delivering messages in order, within the socket.
  • ch271828n
    ch271828n about 4 years
    Is application-layer ACK enough to ensure exactly 0% loss packets? We are developing an application where even 0.001% loss will cause serious trouble. Thanks!
  • Aritra Sur Roy
    Aritra Sur Roy about 2 years
    Websockets use TCP as a transport and that ensures correct delivery and ordering of packets. Did you mean some kind to mechanism in the application layer to find out which response was intended for which request?