-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Duplicate socket.io events #490
Comments
Some of that can definitely be cleaned and shortened, but the point is there. I'd be more than willing to make an official PR with a concise implementation if this is an acceptable solution. |
👍 |
I'd like to also point out that in my example I do not handle socket.io reconnect events. Rooms need to be re-joined after a successful reconnect. |
Here is my implementation of It handles room connections on |
On the issue of socket.io rooms and reconnects. If a client(browser) looses connection and then reconnects, with the implementation above, any further updates will be received. However that doesn't account for any updates that happened while the client was disconnected. I'm wondering what would be more advantageous: a The second option could allow for a reduced dependency on the Thoughts? |
Hi, socket.syncUpdates('room'+$scope.roomId, $scope.messages);
...
function onSave(socket, doc, cb) {
socket.to('room-'+doc.roomId).emit('room-'+doc.roomId+':save', doc);
} VS socket.syncUpdates('room'+$scope.roomId, 'message', $scope.messages);
...
function onSave(socket, doc, cb) {
socket.to('room-'+doc.roomid).emit('message:save', doc);
} I tried it and it works nicely. But maybe I'm completly wrong or it's nonsense. Just tell me. Thank's in advance Note: sorry for my bad english |
Thanks for this, helped me out! |
Using this implementation, how would one go about storing information specific to the individual socket to the database? I want to create a chat where one person speaks with the admin. Having randomly generated rooms made this easier because I could store the room name to the database and exclude others from accessing that room except the admin and the person. How can this be accomplished? |
🍺 goes to you @kingcody |
Thanks @kingcody, I was trying to work through this and finally found this answer after 2 hours of playing around with the mongoose hooks. |
As pointed out to me by a user of this generator. When a socket.io connection is made and the socket is registered to the model's
post('save')
hook, there is no method to remove these listeners. As a side effect as connections continue to be made (reloads or new) the hook's event listener list grows proportionally. For him this is causing an unwanted amount of events to be fired off, and due to his modifications, redundant effects (don't know the details, doesn't affect the issue).I've searched the documentation on mongoose and I don't believe you can remove
middleware
events, at least not easily?With that "assumption" I've rewritten
thing.socket.js
to roughly be:Notice that I only export one function, and it receives
socketio
the socket.io server, not an individualsocket
. This is because I only bind to the pre save hook once at server startup, like so:server/config/socketio.js
I use socket.io rooms to handle the different clients/listeners. Since rooms are managed by the socket.io server I added listeners for
join
andleave
events from the client sockets:The client can then sync updates as usual and only needs to call
socket.emit('join', modelName)
whensyncUpdates
and callsocket.emit('leave', modelName)
whenunsyncUpdates
(in addition to the normal actions). This should have the added benefit of not sending events to clients that they are not subscribed to; instead of the client simply ignoring the events. Also, the server does not have a continually growing list of event listeners on a model's schema; which would most likely appear as a mem leak over time.2¢
EDIT: typo
The text was updated successfully, but these errors were encountered: