<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>rorosyd — Suranyami</title>
    <link>https://suranyami.com/tag:rorosyd</link>
    <description>Polyglot developer, geometric tessellation fan, ambient DJ.</description>
    <pubDate>Sun, 22 Nov 2026 21:27:33 +0000</pubDate>
    <item>
      <title>EventMachine + WebSockets, #rorosyd talk</title>
      <link>https://suranyami.com/eventmachine-websockets-rorosyd-talk?pk_campaign=rss-feed</link>
      <description>January 11 2012, 3:02 PM by David Parry EventMachine + WebSockets This is the notes from a talk I gave at #rorosyd on the 10th of January...</description>
      <content:encoded><![CDATA[<p>January 11 2012, 3:02 PM  by David Parry</p>

<h1 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">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">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">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">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">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">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">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">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">CoffeeScript</h1>

<h2 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">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">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">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">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">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>