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

Finding a websocket session and send message #26

Closed
lucaskuan opened this issue Jun 4, 2018 · 8 comments
Closed

Finding a websocket session and send message #26

lucaskuan opened this issue Jun 4, 2018 · 8 comments
Labels

Comments

@lucaskuan
Copy link

lucaskuan commented Jun 4, 2018

Hi, I was able to find a specific websocket session and send message to the client with following code

Iodine::Websocket.each do |ws|
  if ws.params[:cpid] == cpid
    ws.write "hello"
  end
end

But now something not working as expected. The client keep sending message to server in every 15 seconds but I was not able to find it from the code above. The only change I made to the server related to plezi is changing iodine config from -w 1 to -w 3.
Please help out if there any workaround or better way to find and send message to a specific websocket.

@boazsegev
Copy link
Owner

Hi @lucaskuan ,

Thank you for opening this issue.

I understand your need to send a message to a specific connection and I recommend that you switch to the pub/sub API for this need (the API was updated in iodine 0.6.0, but Plezi still uses iodine 0.4.19 until I release the update I'm working on).

The issue is simple (and the confusion is one of the reasons that each was deprecated in iodine 0.5.0): the each method is process bound (it can't search across process boundaries). I should have been clearer about this in the documentation, as I probably mention this in the wrong place.

When you use iodine -w 2 you are entering cluster mode. In this mode there is more than a single process and each will only access the connections that share the same process.

However, pub/sub is cluster wide and can be easily extended using Redis to support multi-machine horizontal scaling (which allows your messages to cross machine boundaries).

If you give each user a private channel (i.e., subscribe channel: "User.#{@user.id}"), than you could use the pub/sub system to send messages directly to the user's WebSocket or Controller (by adding a block to the subscribe method).

I hope tis helps.

Good Luck!

@lucaskuan
Copy link
Author

Thanks for your instant reply! 🥇
Is that means I could follow the redis setup here https://github.com/boazsegev/iodine/tree/v0.4.15#native-pubsub-with-optional-redis-scaling
and subscribe the user to a channel when he first connected at on_open. So that when I need to send message to this specific user, I could use Iodine.publish 'channel', 'message'. Right?

@boazsegev
Copy link
Owner

Thanks for your instant reply! 🥇

It was mostly luck... but you're welcome :-)

I could follow the redis setup here...

Yes, this is a good option if you are running multiple instances of your application (i.e., running the application on multiple machines or, on Heroku, using multiple dynos).

With Plezi, this can be automated by setting the PL_REDIS_URL environment variable.

Also, please note: if you are running a single process cluster (one call to iodine -w 3), you don't need Redis and you can use iodine's internal pub/sub (it includes inter-process-communication).

In fact, if your running only a single application instance (i.e., one iodine cluster), Redis will only slow the application down.

@lucaskuan
Copy link
Author

I'm playing around with the publish and found out the publish won't work without PL_REDIS_URL. Is the internal pub/sub work out of the box or some configuration is needed?
I'm on plezi 0.5.0 iodine 0.4.14.

@boazsegev
Copy link
Owner

It should work out of the box.

Please update to iodine 0.4.19 and try again.

@boazsegev
Copy link
Owner

@lucaskuan

Please let me know if you still have an issue after upgrading to iodine 0.4.19 (also, please consider upgrading to Plezi 0.5.1).

Thank you for opening this issue.

B.

@lucaskuan
Copy link
Author

Since I'm not ready to do upgrade and testing. Close this issue first and try later

@boazsegev
Copy link
Owner

Okay, thank you :-)

P.S.

Please note that iodine 0.4.19 should be 100% compatible with iodine 0.4.15, the patch version updates only include bug fixes.

It should be easy to upgrade to iodine 0.4.19 and Plezi 0.15.1 (no code changes).

Plezi 0.16.0 and Iodine 0.6.2 are the latest versions and have some significant changes to the API (mostly due to collaborations in an attempt to create a Rack standard API for WebSocket connections and pub/sub).

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

No branches or pull requests

2 participants