Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socketIo wrongly emits event to all clients #2996

Closed
1 task done
admirkadriu opened this issue Jul 4, 2017 · 4 comments · Fixed by #3039
Closed
1 task done

socketIo wrongly emits event to all clients #2996

admirkadriu opened this issue Jul 4, 2017 · 4 comments · Fixed by #3039
Milestone

Comments

@admirkadriu
Copy link

admirkadriu commented Jul 4, 2017

You want to:

  • report a bug

Current behaviour

The code below sends events to all clients. The first event works as expected; the second one sends event to all clients:

let socket = io.to(device.socketId);
socket.emit('transferRequested', {
                                userId: self.identity.userId,
                                profilePicture: self.identity.userInfo.profilePicture,
                                callModel: callModel
                            });
socket.emit('transferRequested1', { 
                                userId: self.identity.userId,
                                profilePicture: self.identity.userInfo.profilePicture,
                                callModel: callModel
                            });

The thing is we use node async/await to simplify our code. So, the same thing happens if we change the code like this:

duplicate(callback) {
        callback();
        callback();
    }

duplicate(async ()=>{
          io.to(socketId).emit('transferRequested', {
                            userId: '123',
                            profilePicture: 'test.jpg',
                            callModel: null,
                            membersState: await self.callParticipantService.getMembersState('123456789098765')
                        });
});

Somehow the second call to await function returns to the scope of the first await call. And the same case as in the first example happens.

image

image

Other information

OS: Windows
node -v 7.10.0
socketIo -v 2.0.3

@admirkadriu admirkadriu changed the title socket.emit sends event to all clients socketIo emits event to all clients Jul 5, 2017
@admirkadriu admirkadriu changed the title socketIo emits event to all clients socketIo wrongly emits event to all clients Jul 5, 2017
@Merraclius
Copy link

What about that issue, @admirkadriu you found solution?

@darrachequesne
Copy link
Member

You can't reuse the socket object :

io.to(device.socketId).emit('transferRequested', {
                                userId: self.identity.userId,
                                profilePicture: self.identity.userInfo.profilePicture,
                                callModel: callModel
                            }); 
// all flags and rooms are cleaned up after each emit
io.to(device.socketId).emit('transferRequested1', { 
                                userId: self.identity.userId,
                                profilePicture: self.identity.userInfo.profilePicture,
                                callModel: callModel
                            });

@admirkadriu
Copy link
Author

admirkadriu commented Aug 2, 2017

@darrachequesne that`s OK.

The bug happens when an event is called fast multiple times from a client and when server emits from an async function and await is used inside data to be sent.

You can emit an event to all clients using the code below.

(example)

duplicate(callback) {
        callback();
        callback();
    }

duplicate(async ()=>{
          io.to(socketId).emit('transferRequested', {
                            userId: '123',
                            profilePicture: 'test.jpg',
                            callModel: null,
                            membersState: await self.callParticipantService.getMembersState('123456789098765')
                        });
});

@admirkadriu
Copy link
Author

@Merraclius yes, by moving await outside the model.

duplicate(callback) {
        callback();
        callback();
    }

duplicate(async ()=>{
          let members = await self.callParticipantService.getMembersState('123456789098765')
          io.to(socketId).emit('transferRequested', {
                            userId: '123',
                            profilePicture: 'test.jpg',
                            callModel: null,
                            membersState:  members
                        });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants