title: How to connect to multiple WebSockets with Ruby?
body: <p>Using <a href="https://github.com/faye/faye-websocket-ruby" rel="nofollow noreferrer">faye-websocket</a> and EventMachine the code looks very similar to faye-websocket's client example:</p>
  
  <pre><code>require 'faye/websocket'
  require 'eventmachine'
  
  def setup_socket(url)
      EM.run {
        ws = Faye::WebSocket::Client.new(url)
  
        ws.on :open do ... end
  
        ws.on :message do ... end
  
        ws.on :close do ... end
      }
  end
  </code></pre>
  
  <p>I'd like to have multiple connections open parallely. I can't simply call <code>setup_socket</code> multiple times as the execution won't exit the <code>EM.run</code> clause. I've tried to run <code>setup_socket</code> multiple times in separate threads as:</p>
  
  <pre><code>urls.each do |url|
      Thread.new { setup_socket(url) }
  end
  </code></pre>
  
  <p>But it doesn't seem to do anyhting as the <code>puts</code> statements don't reach the output.</p>
  
  <p>I'm not restricted to use faye-websocket but it seemed most people use this library. If possible I'd like to avoid multithreading. I'd also not like to lose the possiblity to make changes (e.g. add a new websocket) over time. Therefore moving the iteration of URLs inside the <code>EM.run</code> clause is not desired but instead starting multiple EMs would be more beneficial. I found an <a href="https://stackoverflow.com/questions/6158394/multiple-servers-in-a-single-eventmachine-reactor">example for starting multiple servers via EM</a> in a very clean way. I'm looking for something similar.</p>
  
  <p><strong>How can I connect to multiple WebSockets at the same time?</strong></p>