How to communicate with threads in Ruby?
Asked Answered
A

1

7

I'm building a real time game, mostly chat based, and I need to have many of these chats running concurrently, receiving and sending data through web sockets.

I have been told that instead of spawning one process per game, I should have one process with one thread per game (maybe using Event Machine).

I'm using Juggernaut for the sockets part, it lets me send data to all the players in a game by using a publish/subscribe system: each player subscribes to one game. But how do I send data from each player to that particular game?

I was thinking that I could send the game ID or channel ID from the client to the server, and then send it to the corresponding thread.

But how do I send anything to a thread?

Aphasic answered 6/4, 2012 at 15:35 Comment(1)
Have you considered using Redis? You could perhaps have an action that receives data and stores it in Redis (redis.io).Rabi
S
8

To send data to a thread, you can use Ruby Thread Queue.

Ruby 3.3.0: https://ruby-doc.org/3.3.0/Thread/Queue.html

(If you're reading this and there's a newer version of Ruby, please feel free to edit this answer to update the Ruby version)

Steffin answered 6/4, 2012 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.