# EventMachine or GServer?

**URL:** <https://www.ruby-forum.com/t/eventmachine-or-gserver/168185>\
**Category:** Ruby\
**Created:** [May 17, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/eventmachine-or-gserver/168185 "2009-05-17T23:52:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [May 17, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/eventmachine-or-gserver/168185/1 "2009-05-17T23:52:15Z")

</div>

I wrote my nifty server in GServer, but just stumbled on EventMachine.  
Is EM better than my GServer implementation (i guess it is :))

class GameServer \< GServer  
def initialize(\*args)  
super(\*args)  
end

def serve(io)  
log “Registering #{io} to Dispatcher.”  
dispatcher = Dispatcher.singleton  
dispatcher.register io

```
loop do
  begin
    log "Reading from #{io}"
    str = nil
    Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
    until str.nil?
      str.strip!
      dispatcher.receive io, JSON.load(str)

      Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
    end
  rescue Timeout::Error
  end

  log "Dispatching outgoing messages..."
  dispatcher.each_outgoing_for(io) do |message|
    io.write JSON.dump(message)
    io.write "\n"
    io.flush
  end

  log "Sleeping..."
  sleep CONFIG['sleep_timeout']
end

```

rescue JSON::ParserError  
io.write “Unknown protocol, aborting.\n”  
rescue Exception =\> e  
log “Exception for #{io}: #{e.inspect}”  
end  
end

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [May 18, 2009, 12:33am UTC](https://www.ruby-forum.com/t/eventmachine-or-gserver/168185/2 "2009-05-18T00:33:50Z")

</div>

Hm, the EM implementation…

module GameServer  
def post\_init  
puts “Registering #{self} to Dispatcher.”  
Dispatcher.singleton.register self  
end

def receive\_data(data)  
Dispatcher.singleton.receive self, JSON.load(data) if data != “”  
rescue JSON::ParserError  
send\_data “Unknown protocol, aborting.\n”  
close\_connection\_after\_writing  
end

def send\_message(message)  
send\_data “%s\n” % JSON.dump(message)  
end

def unbind  
Dispatcher.singleton.unregister self  
end  
end

So I guess EM handles loads better (as it’s C) and is easier to use?

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [May 18, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/eventmachine-or-gserver/168185/3 "2009-05-18T13:44:36Z")

</div>

On May 17, 2009, at 5:34 PM, ArtÅ«ras Å lajus wrote:

> rescue JSON::ParserError  
> end  
> end
> 
> So I guess EM handles loads better (as it’s C) and is easier to use?

EM is certainly very fast but I don’t know if it is easier to use. The  
documentation is hard to understand and the examples are all so  
simplistic it is often difficult to see how to write a complex client  
or server.

I suggest downloading projects that have used EM (like Thin) and  
learning good techniques from that code.

cr

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [May 18, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/eventmachine-or-gserver/168185/4 "2009-05-18T17:13:35Z")

</div>

Chuck R. wrote:

> EM is certainly very fast but I don’t know if it is easier to use. The  
> documentation is hard to understand and the examples are all so  
> simplistic it is often difficult to see how to write a complex client  
> or server.  
> Well, it’s good enough for me. I guess I’ll stick to EM then 🙂

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [May 18, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/eventmachine-or-gserver/168185/5 "2009-05-18T18:31:33Z")

</div>

On Mon, May 18, 2009 at 7:42 AM, Chuck R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> it is often difficult to see how to write a complex client or server.
> 
> I suggest downloading projects that have used EM (like Thin) and learning  
> good techniques from that code.
> 
> cr

I’d be very open to hearing suggestions on how to improve the EM  
documentation. Feel free to email me directly.
