Reliable message communication within WebSocket.

yuanoook
yuanoook 发布于 2017-01-04 19:32:38 浏览:133 类型:原创 - 随笔 分类:性能优化 - 待整理 二维码: 作者原创 版权保护

##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
``` javascript
socket.send('message')
```
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 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.
[MessageUniqueId:Receiver:Received(false), ...]

Receiver's message storage list:
// All the message received
[MessageUniqueId, ...]
* 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:
[MessageUniqueId:Sender:SenderKnowItIsReceived(false),...]
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:
Step1:
Sender(send may more than once) >>> messageWithUniqueId >>> Receiver
Step2:
Sender <<< I've gotten the message <<< Receiver (received may more than once, but reaction just once)
Step3:(optional)
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 :)
标签:
z
给个赞 1 人点赞
收藏 0 人收藏
评论 已有 0 条评论;以下用户言论只代表其个人观点,不代表 前端网(QDFuns) 的观点或立场。
登录 以后才能发表评论
最新评论
还没有任何评论呢,赶紧抢先来一发吧!
yuanoook yuanoook 作者

享受平凡 | 追求卓越

作者最新