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?
But from Ruby 1.9 onwards, threading is performed by the operating system. The two threads running in the same Ruby application can never be truly concurrent. In Ruby, a multi-threaded program is created with the help of Thread class and a new thread is created by calling a block, i.e Thread. new.
Ruby makes it easy to write multi-threaded programs with the Thread class. Ruby threads are a lightweight and efficient way to achieve concurrency in your code.
In particular, Ruby concurrency is when two tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean, though, that they'll ever both be running at the same instant (e.g., multiple threads on a single-core machine).
Calling Thread. join blocks the current (main) thread. However not calling join results in all spawned threads to be killed when the main thread exits. How does one spawn persistent children threads in Ruby without blocking the main thread? Here's a typical use of join.
To send data to a thread, you can use Ruby Queue:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/thread/rdoc/Queue.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With