Reliable message communication within WebSocket.
## WebSocket looks pretty, but I don’t use it now.
To transfer messages between clients and server, WebSocket is more efficient and faster than normal HTTP.
But, WebSocket is unreliable, you don’t know anything if the clients missed some messages because the connection was broken in a while. How about if the messages are critical for your clients.
You can’t stand with any mistake may be appeared in you application system. So, you use HTTP Pooling strategy.
It’s sorrowful that when you know a new and better technology but can’t use it with enough confidence :(
## How to make WebSocket reliable?
The naturally way to use WebSocket is as follow
But it’s not enough for [reliable] communications.
The [reliable] part means that the message receiver received the message successfully.
How to make sure the receiver have received the message.
It’s simple. Just let the receiver tell the sender: “Ok, I have gotten the message.” If the sender didn’t know the receiver already read the message, send it later again. That’s all :)
But, how to prevent the receiver reaction with the same message more than once. The answer is [uniqueId]. After the receiver gets a message. Checks the uniqueId, if the message already read. Reply the sender “Ok, I have gotten the message. Don’t send it to me again. :)”. Then the sender mark the message as [received(true)], and never re-send the message again.
## Essential items to make reliable message communication application:
Sender’s message storage list:
// If the message marked as Received(true), there’s no need to save it.
Receiver’s message storage list:
// All the message received
* The receiver’s storage will become huge after a long time. And checking a message existed-status in a long list will be slow. OK, you can do more. Let the sender replay the receiver “I know you’ve received the message!”.
Receiver’s message storage thinner list:
In this list, the message sender may send the message again because the sender doesn’t make sure the receiver already received it. Keep the list to prevent the receiver reaction more than once.
## A reliable message communication:
Sender(send may more than once) >>> messageWithUniqueId >>> Receiver
Sender <<< I’ve gotten the message <<< Receiver (received may more than once, but reaction just once)
Sender >>>>>>> I know you received the message >>>>>>> Receiver
## It’s about reliable, not about WebSocket.
A: Hey, did you hear me?
B: Yes, I did.
Make sure your listener gets what you say. It’s just a communication skill. WebSocket is just a message channel. You can use any other channels if you like, even Express Mail Service :)
That’s all. I hope you like it :)