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

SailsJS instance does not know the room subscribers from another sails instance #2593

Closed
ghost opened this issue Jan 27, 2015 · 8 comments
Closed

Comments

@ghost
Copy link

ghost commented Jan 27, 2015

Situation: We've got 2 SailJS instances ( 2 dyno's on Heroku ).
Sockets are connected and situated in Redis.

On instance 1: First join a room with sails.sockets.join(socket, "aRoom");
On instance 1: Broadcast data: sails.sockets.broadcast(roomName, eventName, data);
When one listens to events on this room, events are received, properly.

When one executes: sails.sockets.subscribers("aRoom"); an array of socket ids is returned.
We can execute this method on both instances, and get the same result.

Now comes the tricky part:
We restart instance 2 for example, (stop/start).
And execute : sails.sockets.subscribers("aRoom"); again.
Now sails does not return subscribers anymore, but we are still able to broadcast to that room?
( It seems the method sails.sockets.subscribers is not aware of the shared sockets in Redis )


Update:
Seems when one subscribes to a model, e.g:
On instance 1: UserAccount.subscribe(socket, 6)
On instance 2: UserAccount.subscribers(6) returns an Array with one socketId, this is ok.
So, it seems Redis does the job, in being an intermediary for socket sessions.

When one restarts instance 2 for instance, and does: UserAccount.subscribers(6), this wil return an empty Array now!??
Besides this, one can do:
UserAccount.message(6, {test:1234}); and this is properly sent and received at the client, but no subscribers?

I'm doing something wrong maybe?

@ghost
Copy link
Author

ghost commented Jan 27, 2015

I maybe answering my onw issue, need a bit of testing still.

It seems I did not configure Redis correctly for the sockets... :-S.

I'll get back on this tomorrow.

@ghost
Copy link
Author

ghost commented Jan 28, 2015

Ok,

Seems I configured Redis for the socket.js in config correctly.
When one sends a [Model].message(), it gets pushed to Redis, great!
The correct dyno pulls the message from Redis and sends it to the correct client (connected with this dyno). Even if dyno1 executes the [Model].message(); statement, dyno2 sends it to its connected client.

The same for broadcasting in a room, also great.

But In my opinion, when one does a subscriber check like:
[Model].subscribers(modelId);
It should return the subscribers connected to all dynos, right?
This is wat I would expect, because [Model].message(); does the same.

I'm doing something wrong, maybe, but is this correct?
It seems its difficult to see if a client is connected with one of the dyno's programmatically.
This would be a great feature!

SailsJS rules by the way

@mikermcneil
Copy link
Member

@kaasdude Thanks for coming back and answering :)

But In my opinion, when one does a subscriber check like:
[Model].subscribers(modelId);
It should return the subscribers connected to all dynos, right?
This is wat I would expect, because [Model].message(); does the same.

I'm doing something wrong, maybe, but is this correct?
It seems its difficult to see if a client is connected with one of the dyno's programmatically.
This would be a great feature!

Indeed. I've been talking to @rauchg about this-- it's complex, but getting close: socketio/socket.io-redis-adapter#15 (comment)

@mikermcneil
Copy link
Member

(my bad, I'll wait and let you close when you've got it sorted)

@mikermcneil
Copy link
Member

mikermcneil commented Jan 28, 2015

@mikermcneil
Copy link
Member

(argh sry keep doing that)

@ghost
Copy link
Author

ghost commented Feb 11, 2015

I hope you guys are willing to fix this (even if this issue already is closed now), we really could use this.
I can imagine this could be a complex issue.
Thanks in advance!

@mikermcneil
Copy link
Member

To follow up for future reference:

The best way to go about this is to never actually track the socket ids at all-- instead, use your database. Broadcasting works across multiple servers thanks to socket.io-redis, and joining rooms works even with multiple servers thanks to our implementation of the admin bus in sails-hook-sockets.

You technically can track socket ids in your app (i.e. store them on your model), but if possible, it's best to treat them as transient. So, for example, if you have a "Canvas" that can be viewed by multiple people, and your UI shows coviewers, then you can track the user id of the coviewer (rather than their socket id). This is also more flexible when they change devices, etc. (And if you support guests that don't have user ids yet, but want to show them as coviewers, manage "Guest" records for them). tldr; avoid using socket.io as a database, and avoid using socket ids to track users or guests. Instead, use your app's models to store and retrieve data, and track users by their user id and, if necessary, track guests by their guest id

For more tips, see http://sailsjs.org/documentation/concepts/realtime

PS. @kaasdude thanks again for your help in figuring out the right solution!

@balderdashy balderdashy locked and limited conversation to collaborators Nov 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant