title: event machine websocket server: how to push periodic messages to client?
body: <p>I would like to use ruby language gem em-websocket:
  <a href="https://github.com/igrigorik/em-websocket" rel="nofollow">https://github.com/igrigorik/em-websocket</a>
  to run a websocket server above eventmachine.</p>
  
  <p>I run susccessfully the echo server demo:</p>
  
  <pre><code>EM.run {
    EM::WebSocket.run(:host =&gt; "0.0.0.0", :port =&gt; 8080) do |ws|
      ws.onopen { |handshake|
        puts "WebSocket connection open"
  
        ws.send "Hello Client, you connected to #{handshake.path}"
      }
  
      ws.onclose { puts "Connection closed" }
  
      ws.onmessage { |msg|
        puts "Recieved message: #{msg}"
        ws.send "Pong: #{msg}"
      }
    end
  }
  </code></pre>
  
  <p>now I'd like to develop a demo where the server could push every n msecs (random value) some data through a websocket message to a connected client... virtually something like:</p>
  
  <pre><code>def periodic_push
      # random elapsed: from 100 milliseconds to 6 seconds
      msec_value =  (rand(100..6000) / 1000.0 )
      message = "time: #{Time.now} value: #{msec_value.to_s}"
  
      ws.send message
      puts message
  
      sleep (msec_value)
  end
  </code></pre>
  
  <p>I'm aware I can NOT use sleep() system call inside the EM loop,
  but I cant' figure out how to insert a periodic event in the <code>EM::WebSocket loop</code>;
  maybe with  <code>EventMachine::PeriodicTimer</code> ? how ?</p>
  
  <p>Someone can help me with a code example chunck ?
  thanks!
  giorgio</p>