socket io broadcast, rooms and acknowledgement function

丶灬走出姿态 提交于 2019-12-24 09:16:03

问题


we are looking at socket io implementation for a chat application. Finding acknowledgement support to handle missing messages while broadcast we are looking at acknowledgement support. as per documentation socket io does not have support for callbacks in broadcast / rooms.

e.g. in "Room 1" we send broadcast message to all sockets within that room. how we check without call back that some users/sockets missed the message. and how we will handle that in system.

below code does not work.

io.sockets.in(data.room).emit('message', data, function(responseData){
            console.log(responseData);
        });

according to below issue https://github.com/socketio/socket.io-redis/issues/30 Callbacks are not supported when broadcasting.

what are the other methods to handle this scenario.


回答1:


In order to solve your problem, the messages for a room need to be persisted somewhere, and then re-sent to individual clients as needed.

The most obvious place to store messages is server-side, in a datastore (e.g. Redis). Store each conversation effectively as a list of events, appending new events as they happen.

A simple scheme works as follows:

  • Each broadcast message has a UUID attached to it. When the server handles a new message, it appends the message to the list for that 'room'.
  • When a client connects/re-connects, it sends a message (e.g. 'LAST_MESSAGE_RECEIVED') indicating the UUID of the last message it received.
  • When the server receives one of these 'LAST_MESSAGE_RECEIVED' messages, it checks if that is the latest message for the room, and if not, it emits a message just to that individual socket with an array of the missed messages. The client is now back up-to-date.

Alternative: if you don't need to keep a history after a conversation ends, you could be clever and use the fact that other clients are already storing the messages, and ask the clients to re-send messages in a peer-to-peer kind of way. This avoids you needing to have your own server-side datastore.



来源:https://stackoverflow.com/questions/43186636/socket-io-broadcast-rooms-and-acknowledgement-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!