<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>rorosyd &amp;mdash; Suranyami</title>
    <link>https://suranyami.com/tag:rorosyd</link>
    <description>Polyglot developer, geometric tessellation fan, ambient DJ.</description>
    <pubDate>Fri, 17 Apr 2026 07:08:48 +0000</pubDate>
    <item>
      <title>EventMachine + WebSockets, #rorosyd talk</title>
      <link>https://suranyami.com/eventmachine-websockets-rorosyd-talk?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[January 11 2012, 3:02 PM  by David Parry&#xA;&#xA;EventMachine + WebSockets&#xA;&#xA;This is the notes from a talk I gave at #rorosyd on the 10th of January 2012.&#xA;&#xA;The demo code is available at: https://github.com/suranyami/socketdemo&#xA;&#xA;So, what’s EventMachine?&#xA;&#xA;Reactor pattern&#xA;Extremely high performance&#xA;Doesn’t need threads, concurrency&#xA;Addresses the C10K problem&#xA;Callbacks&#xA;Fast, low memory overhead&#xA;&#xA;EventMachine supports lots of protocols!&#xA;&#xA;There is a great list of currently implemented protocols at https://github.com/eventmachine/eventmachine/wiki/Protocol-Implementations which includes:&#xA;&#xA;HeaderAndContent&#xA;SMTPServer&#xA;Stomp&#xA;Socks4&#xA;ObjectProtocol&#xA;SASLauth&#xA;LineAndText&#xA;LineText2&#xA;HTTPClient&#xA;HTTPClient2&#xA;AMQP&#xA;MySQL&#xA;SMTP&#xA;Postgres&#xA;MemCache&#xA;XMPP&#xA;DNS&#xA;PowerDNS&#xA;ICMP&#xA;XML Push Parser&#xA;Redis&#xA;MongoDB&#xA;CouchDB&#xA;Beanstalk&#xA;SNMP&#xA;HTTPRequest&#xA;HTTPServer&#xA;PubSubHubbub&#xA;Proxy&#xA;WebSocket&#xA;SMPP&#xA;RPC&#xA;IRC&#xA;Spec&#xA;Cassandra&#xA;Thrift&#xA;Solr&#xA;Syslog&#xA;Amazon S3&#xA;OSCAR (AIM, ICQ)&#xA;RServe&#xA;SSH&#xA;&#xA;How to learn about EventMachine?&#xA;&#xA;PeepCode&#xA;&#xA;EventMachine Introduction&#xA;&#xA;Example of a Trivial Telnet Server&#xA;&#xA;require &#39;eventmachine&#39;&#xA;class Echo &lt; EM::Connection&#xA;  def receivedata(data)&#xA;    senddata(data)&#xA;  end&#xA;end&#xA;&#xA;EM.run do&#xA;  EM.startserver(“0.0.0.0”, 10000, Echo)&#xA;end&#xA;&#xA;So, what’s a WebSocket?&#xA;&#xA;Used for “push”, real-time bidirectional updates&#xA;HTML 5, proposed RFC, too.&#xA;In all modern browsers Chrome Safari, Safari Mobile Firefoxx but NOT Android browser (FFS! Why?) ** IE 10, but not IE 6-9 (who cares!)&#xA;iOS native library + elsewhere&#xA;Combining EventMachine + WebSocket = em-websocket&#xA;&#xA;Where would I use it?&#xA;&#xA;Chat&#xA;Multiplayer Games&#xA;Real-time dataviz&#xA;Real-time news feeds&#xA;Real-time anything&#xA;&#xA;Trivial WebSocket server&#xA;&#xA;require &#39;eventmachine&#39;&#xA;require &#39;em-websocket&#39;&#xA;&#xA;conf = {:host =  “0.0.0.0”, :port =  8080}&#xA;&#xA;EM.run {&#xA;  EventMachine::WebSocket.start(conf) do |ws|&#xA;    ws.onopen {ws.send &#34;Hello Client&#34;}&#xA;    ws.onclose { puts &#34;Connection closed&#34; }&#xA;    ws.onmessage {|msg| ws.send &#34;Echo: #{msg}&#34;}&#xA;end }&#xA;&#xA;A Less Trivial example&#xA;&#xA;So, I wanted something to demo that wasn’t trying to do to much but demonstrated the real-time nature of WebSockets, so I built this little socketdemo site:&#xA;&#xA;Sinatra serves HTML, JS (CoffeeScript), CSS&#xA;Running an EM-WebSocket server&#xA;Each guest decides whether a block is green or red (default’s white)&#xA;Joining/leaving adds/removes blocks&#xA;New guest gets snapshot of world&#xA;&#xA;Useless, but non-trivial!&#xA;&#xA;CoffeeScript&#xA;&#xA;Assign an id to socket/guest&#xA;&#xA;EM-Websocket Server:&#xA;&#xA;class Demo constructor: –  if WebSocket?&#xA;      window.socket = new WebSocket(&#34;ws://192.168.1.3:7070&#34;)&#xA;    else&#xA;      window.socket = new MozWebSocket(&#34;ws://192.168.1.3:7070&#34;)&#xA;&#xA;Register ourselves&#xA;&#xA;CoffeeScript:&#xA;&#xA;window.socket.onopen = –  window.socket.send JSON.stringify({kind: “register”})&#xA;&#xA;EM-Websocket Server:&#xA;&#xA;socket.onmessage do |msg|&#xA;  puts &#34;Server Received #{msg}&#34;&#xA;  id = @socketssocket&#xA;  incoming = ActiveSupport::JSON.decode(msg)&#xA;  case incoming[“kind”]&#xA;    when &#34;register&#34;&#xA;      socket.send(registermessage(id).tojson)&#xA;      broadcast(id)&#xA;    else&#xA;      ## Send “add” event to everyone&#xA;&#xA;EM-Websocket Server:&#xA;&#xA;def sendtoall(message) @sockets.each do |destination|`&#xA;    destination.send(message.tojson)&#xA;end&#xA;&#xA;CoffeeScript:&#xA;&#xA;window.socket.onmessage = (mess) –  data = jQuery.parseJSON(mess.data)&#xA;  switch data[“kind”]&#xA;  when &#34;add&#34;&#xA;      window.addplayer data[&#34;id&#34;], data[&#34;color&#34;]&#xA;&#xA;Send click events&#xA;&#xA;$(“#red”).click (e) =  window.socket.send JSON.stringify {kind: “update”, color: “red”}&#xA;&#xA;$(“#green”).click (e) =  window.socket.send JSON.stringify {kind: “update”, color: “green”}&#xA;&#xA;Server gets a message&#xA;&#xA;socket.onmessage do |msg|&#xA;  id = @socketssocket incoming = ActiveSupport::JSON.decode(msg)&#xA;  case incoming[“kind”]&#xA;    when “register”&#xA;      socket.send(registermessage(id).tojson)&#xA;      broadcast(id)&#xA;    else&#xA;      if incoming[&#34;color&#34;]&#xA;        color = incoming[&#34;color&#34;]&#xA;        message = {&#34;kind&#34; =  &#34;update&#34;, &#34;id&#34; =  id, &#34;color&#34; =  color}&#xA;        @socketssocket = color&#xA;        sendtoall(message)&#xA;    end&#xA;  end&#xA;end&#xA;&#xA;Live Demo&#xA;&#xA;Then I gave a live demo where I used my laptop as an unsecured base station, and people connected to it with their iPhones and started clicking on the red/green buttons:&#xA;&#xA;    wifi:  suranyami&#xA;    (no password)&#xA;    url:  test.local:4567&#xA;&#xA;How to do testing with EventMachine?&#xA;&#xA;    em-spec&#xA;    https://github.com/joshbuddy/em-spec&#xA;&#xA;This demo code viewable here: https://github.com/suranyami/socketdemo&#xA;&#xA;September 11th, 2013 2:19pm&#xA;&#xA;a href=&#34;https://remark.as/p/suranyami.com/eventmachine-websockets-rorosyd-talk&#34;Discuss.../a]]&gt;</description>
      <content:encoded><![CDATA[<p>January 11 2012, 3:02 PM  by David Parry</p>

<h1 id="eventmachine-websockets" id="eventmachine-websockets">EventMachine + WebSockets</h1>

<p>This is the notes from a talk I gave at <a href="https://suranyami.com/tag:rorosyd" class="hashtag"><span>#</span><span class="p-category">rorosyd</span></a> on the 10th of January 2012.</p>

<p>The demo code is available at: [<a href="https://github.com/suranyami/socket_demo][">https://github.com/suranyami/socket_demo][</a>]</p>

<h2 id="so-what-s-eventmachine" id="so-what-s-eventmachine">So, what’s EventMachine?</h2>
<ul><li>Reactor pattern</li>
<li>Extremely high performance</li>
<li>Doesn’t need threads, concurrency</li>
<li>Addresses the C10K problem</li>
<li>Callbacks</li>
<li>Fast, low memory overhead</li></ul>

<h2 id="eventmachine-supports-lots-of-protocols" id="eventmachine-supports-lots-of-protocols">EventMachine supports lots of protocols!</h2>

<p>There is a great list of currently implemented protocols at <a href="https://github.com/eventmachine/eventmachine/wiki/Protocol-Implementations">https://github.com/eventmachine/eventmachine/wiki/Protocol-Implementations</a> which includes:</p>
<ul><li>HeaderAndContent</li>
<li>SMTPServer</li>
<li>Stomp</li>
<li>Socks4</li>
<li>ObjectProtocol</li>
<li>SASLauth</li>
<li>LineAndText</li>
<li>LineText2</li>
<li>HTTPClient</li>
<li>HTTPClient2</li>
<li>AMQP</li>
<li>MySQL</li>
<li>SMTP</li>
<li>Postgres</li>
<li>MemCache</li>
<li>XMPP</li>
<li>DNS</li>
<li>PowerDNS</li>
<li>ICMP</li>
<li>XML Push Parser</li>
<li>Redis</li>
<li>MongoDB</li>
<li>CouchDB</li>
<li>Beanstalk</li>
<li>SNMP</li>
<li>HTTPRequest</li>
<li>HTTPServer</li>
<li>PubSubHubbub</li>
<li>Proxy</li>
<li>WebSocket</li>
<li>SMPP</li>
<li>RPC</li>
<li>IRC</li>
<li>Spec</li>
<li>Cassandra</li>
<li>Thrift</li>
<li>Solr</li>
<li>Syslog</li>
<li>Amazon S3</li>
<li>OSCAR (AIM, ICQ)</li>
<li>RServe</li>
<li>SSH</li></ul>

<h2 id="how-to-learn-about-eventmachine" id="how-to-learn-about-eventmachine">How to learn about EventMachine?</h2>

<p><a href="https://peepcode.com/products/eventmachine">PeepCode</a></p>

<p><a href="http://everburning.com/wp-content/uploads/2009/02/eventmachine_introduction_10.pdf">EventMachine Introduction</a></p>

<h2 id="example-of-a-trivial-telnet-server" id="example-of-a-trivial-telnet-server">Example of a Trivial Telnet Server</h2>

<pre><code class="language-ruby">require &#39;eventmachine&#39;
class Echo &lt; EM::Connection
  def receive_data(data)
    send_data(data)
  end
end

EM.run do
  EM.start_server(“0.0.0.0”, 10000, Echo)
end
</code></pre>

<h2 id="so-what-s-a-websocket" id="so-what-s-a-websocket">So, what’s a WebSocket?</h2>
<ul><li>Used for “push”, real-time bidirectional updates</li>
<li>HTML 5, proposed RFC, too.</li>
<li>In all modern browsers <strong>Chrome</strong> Safari, Safari Mobile <strong>Firefoxx</strong> but NOT Android browser (FFS! Why?) ** IE 10, but not IE 6-9 (who cares!)</li>
<li>iOS native library + elsewhere</li>
<li>Combining EventMachine + WebSocket = em-websocket</li></ul>

<h2 id="where-would-i-use-it" id="where-would-i-use-it">Where would I use it?</h2>
<ul><li>Chat</li>
<li>Multiplayer Games</li>
<li>Real-time dataviz</li>
<li>Real-time news feeds</li>
<li>Real-time anything</li></ul>

<h2 id="trivial-websocket-server" id="trivial-websocket-server">Trivial WebSocket server</h2>

<pre><code class="language-ruby">require &#39;eventmachine&#39;
require &#39;em-websocket&#39;

conf = {:host =&gt; “0.0.0.0”, :port =&gt; 8080}

EM.run {
  EventMachine::WebSocket.start(conf) do |ws|
    ws.onopen {ws.send &#34;Hello Client&#34;}
    ws.onclose { puts &#34;Connection closed&#34; }
    ws.onmessage {|msg| ws.send &#34;Echo: #{msg}&#34;}
end }
</code></pre>

<h2 id="a-less-trivial-example" id="a-less-trivial-example">A Less Trivial example</h2>

<p>So, I wanted something to demo that wasn’t trying to do to much but demonstrated the real-time nature of WebSockets, so I built this little socket_demo site:</p>
<ul><li>Sinatra serves HTML, JS (CoffeeScript), CSS</li>
<li>Running an EM-WebSocket server</li>
<li>Each guest decides whether a block is green or red (default’s white)</li>
<li>Joining/leaving adds/removes blocks</li>
<li>New guest gets snapshot of world</li></ul>

<p>Useless, but non-trivial!</p>

<h1 id="coffeescript" id="coffeescript">CoffeeScript</h1>

<h2 id="assign-an-id-to-socket-guest" id="assign-an-id-to-socket-guest">Assign an id to socket/guest</h2>

<p>EM-Websocket Server:</p>

<pre><code class="language-coffeescript">class Demo constructor: –&gt;
    if WebSocket?
      window.socket = new WebSocket(&#34;ws://192.168.1.3:7070&#34;)
    else
      window.socket = new MozWebSocket(&#34;ws://192.168.1.3:7070&#34;)
</code></pre>

<h2 id="register-ourselves" id="register-ourselves">Register ourselves</h2>

<p>CoffeeScript:</p>

<pre><code class="language-coffeescript">window.socket.onopen = –&gt;
  window.socket.send JSON.stringify({kind: “register”})
</code></pre>

<p>EM-Websocket Server:</p>

<pre><code class="language-ruby">socket.onmessage do |msg|
  puts &#34;Server Received #{msg}&#34;
  id = @sockets[socket][“id”]
  incoming = ActiveSupport::JSON.decode(msg)
  case incoming[“kind”]
    when &#34;register&#34;
      socket.send(register_message(id).to_json)
      broadcast(id)
    else
      ## Send “add” event to everyone
</code></pre>

<p>EM-Websocket Server:</p>

<pre><code class="language-ruby">def send_to_all(message) @sockets.each do |destination|`
    destination.send(message.to_json)
end

CoffeeScript:

```coffeescript
window.socket.onmessage = (mess) –&gt;
  data = jQuery.parseJSON(mess.data)
  switch data[“kind”]
  when &#34;add&#34;
      window.add_player data[&#34;id&#34;], data[&#34;color&#34;]
</code></pre>

<h2 id="send-click-events" id="send-click-events">Send click events</h2>

<pre><code class="language-coffescript">$(“#red”).click (e) =&gt; window.socket.send JSON.stringify {kind: “update”, color: “red”}

$(“#green”).click (e) =&gt; window.socket.send JSON.stringify {kind: “update”, color: “green”}
</code></pre>

<h2 id="server-gets-a-message" id="server-gets-a-message">Server gets a message</h2>

<pre><code class="language-ruby">socket.onmessage do |msg|
  id = @sockets[socket][“id”] incoming = ActiveSupport::JSON.decode(msg)
  case incoming[“kind”]
    when “register”
      socket.send(register_message(id).to_json)
      broadcast(id)
    else
      if incoming[&#34;color&#34;]
        color = incoming[&#34;color&#34;]
        message = {&#34;kind&#34; =&gt; &#34;update&#34;, &#34;id&#34; =&gt; id, &#34;color&#34; =&gt; color}
        @sockets[socket][&#34;color&#34;] = color
        send_to_all(message)
    end
  end
end
</code></pre>

<h2 id="live-demo" id="live-demo">Live Demo</h2>

<p>Then I gave a live demo where I used my laptop as an unsecured base station, and people connected to it with their iPhones and started clicking on the red/green buttons:</p>

<p>    wifi:  suranyami
    (no password)
    url:  test.local:4567</p>

<h2 id="how-to-do-testing-with-eventmachine" id="how-to-do-testing-with-eventmachine">How to do testing with EventMachine?</h2>

<p>    em-spec
    <a href="https://github.com/joshbuddy/em-spec">https://github.com/joshbuddy/em-spec</a></p>

<p>This demo code viewable here: [<a href="https://github.com/suranyami/socket_demo][">https://github.com/suranyami/socket_demo][</a>]</p>

<p>September 11th, 2013 2:19pm</p>

<p><a href="https://remark.as/p/suranyami.com/eventmachine-websockets-rorosyd-talk">Discuss...</a></p>
]]></content:encoded>
      <guid>https://suranyami.com/eventmachine-websockets-rorosyd-talk</guid>
      <pubDate>Mon, 12 Jul 2021 05:23:03 +0000</pubDate>
    </item>
  </channel>
</rss>