当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - Reasons socket.io emit not receiving messages?

问题描述:

I'm working on an angular/node app where people can have many 1:1 chats with other users (like Whatsapp without groups) using socket.io and btford's angular-socket module (https://github.com/btford/angular-socket-io). Right now A) a client joins a socket.io room using emit. The client code is:

mySocket.emit('joinroom', room);

Server code is:

socket.on('joinroom', function (room){

socket.join(room);

});

B) chat messages are sent to server via emit. Client code is

mySocket.emit('sendmsg', data, function(data){

console.log(data);

});

and C) the server should send messages to others in the room via broadcast. Server code is:

socket.on('sendmsg', function (text, room, sender, recipient, timestamp) {

// Some code here to save message to database before broadcasting to other users

console.log('This works');

socket.broadcast.to(room).emit('relaymsg', msg);

});

Client code is

$scope.$on('socket:relaymsg', function(event, data) {

console.log('This only sometimes works');

// do stuff to show that message was received

});

A and B seem to work fine, but C seems to be very unreliable. The server code seems to be ok, but the client does not seem to receive the message. Sometimes it works, and sometimes it does not. ie 'This works' always shows up, but 'This only sometimes works' does not always show up.

1) Any thoughts on what could be causing this issue? Are there any errors in my code?

2) Is broadcast and rooms the right way to be setting this up if there are many users, all of which can have multiple 1:1 chats with other users?

In case it helps, this is the factory code for the angular-socket module

.factory('mySocket', function (socketFactory, server) {

var socket = socketFactory({

ioSocket: io.connect(server)

});

socket.forward('relaymsg');

return socket;

});

Appreciate any help you can provide!! Thanks in advance!

网友答案:

Thanks everyone for the comments, I believe I found the main issues. There were two things I think causing problems:

1) The bigger issue I think is that I'm use node clusters, and as a result users might join rooms on different workers and not be able to communicate with each other. I've ended up adding sticky sessions and Redis per the instructions here: http://socket.io/docs/using-multiple-nodes/

Sticky sessions is pretty useful, just as an FYI since the docs don't mention it, the module automatically creates workers and re-spawns them if killed

I couldn't find a ton of examples of how to implement sticky+redis since socket.io 1.0 is relatively new and seems to deal with Redis differently from prior versions, but these were very helpful:

https://github.com/Automattic/socket.io-redis/issues/31

https://github.com/evilstudios/chat-example-cluster/blob/master/index.js

2) Every time the user closed their phone it would disconnect them from the chat room, even if the chat room was the last screen open on the phone

Hope that helps people in the future!

分享给朋友:
您可能感兴趣的文章:
随机阅读: