# Problem with eventmachine

**URL:** <https://www.ruby-forum.com/t/problem-with-eventmachine/69182>\
**Category:** Ruby\
**Created:** [September 19, 2006, 6:13am UTC](https://www.ruby-forum.com/t/problem-with-eventmachine/69182 "2006-09-19T06:13:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 19, 2006, 6:13am UTC](https://www.ruby-forum.com/t/problem-with-eventmachine/69182/1 "2006-09-19T06:13:57Z")

</div>

I have following code, the problem is, when i start a connection to  
the socket through telnet, the data send by the server i get as  
usual, but when i try to do the same through ruby code, the data send  
by the server won’t come to the client…until the server is shutdown.

def receive\_data data  
data = data.chomp.strip  
if is\_symbol?(data)  
add\_symbol(data)  
local\_connection = self  
start\_send\_thread(local\_connection)  
end  
end

def start\_send\_thread(local\_connection)  
unless @send\_started  
p “Trying to send the data”  
@send\_started = true  
@local\_thread = Thread.new do  
loop do  
broadcast\_data(local\_connection)  
sleep(0.1)  
p “I am running”  
end  
end  
end  
end

def broadcast\_data(local\_connection)  
for x in @local\_symbols  
local\_connection.send\_data “#{x}”+rand(10).to\_s  
end  
end

def unbind  
p “Client closed the connection”  
Thread.kill(@local\_thread)  
end

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 19, 2006, 6:35am UTC](https://www.ruby-forum.com/t/problem-with-eventmachine/69182/2 "2006-09-19T06:35:59Z")

</div>

On 9/19/06, hemant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have following code, the problem is, when i start a connection to  
> the socket through telnet, the data send by the server i get as  
> usual, but when i try to do the same through ruby code, the data send  
> by the server won’t come to the client…until the server is shutdown.

Ok that was one stupid wtf…  
i forgot to put “\n” in the end of the string, i guess.

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 19, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/problem-with-eventmachine/69182/3 "2006-09-19T14:04:42Z")

</div>

On 9/19/06, Francis C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you really want to hammer your telnet client with random data based  
> on @local\_symbols, use EventMachine#add\_periodic\_timer rather than  
> spinning a thread.

Thanks for replying.  
Random values were just the placeholders, the actual data would be  
more important i guess. But my understanding of add\_periodic\_timer  
says that, it would be global to entire program. But what i want is  
basically, a method which would be client specific. By being client  
specific, I mean…each connection, should have a callback, which  
would start sending the data. Please correct me, If i am wrong.

Basically what i want is…push the data to the clients  
asynchronously. Using recieve\_data I can change @local\_symbols and  
hence the data pushed to the client should change accordingly.

So, How do i do this?

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 19, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/problem-with-eventmachine/69182/4 "2006-09-19T13:08:38Z")

</div>

On 9/19/06, hemant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have following code, the problem is, when i start a connection to  
> the socket through telnet, the data send by the server i get as  
> usual, but when i try to do the same through ruby code, the data send  
> by the server won’t come to the client…until the server is shutdown.

Two points:  
First, if you’re connecting to this program using telnet, the behavior  
will depend on the platform that you run telnet on. The telnet client  
on Windows sends data byte-by-byte as you type it in. On Linux, Mac or  
other Unix, telnet buffers the data line by line.

Second, and extremely important: you do _not_ need to spin a thread to  
send your data. The whole point of EventMachine is that it allows you  
to handle many connections at once, without using threads. Threads  
slow your program down and make it harder to debug.

If you really want to hammer your telnet client with random data based  
on @local\_symbols, use EventMachine#add\_periodic\_timer rather than  
spinning a thread.

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 19, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/problem-with-eventmachine/69182/5 "2006-09-19T19:50:45Z")

</div>

Sorry if I missed you, Hemant- I was hoping to get a bit more  
information on your home-grown data-vending server. If the protocol is  
appropriate for EM, then you can implement your push server without  
spinning any threads.

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 19, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/problem-with-eventmachine/69182/6 "2006-09-19T14:52:47Z")

</div>

On 9/19/06, hemant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Basically what i want is…push the data to the clients  
> asynchronously. Using recieve\_data I can change @local\_symbols and  
> hence the data pushed to the client should change accordingly.
> 
> So, How do i do this?

The answer depends on when your asynch data becomes available. If it  
happens  
on a time basis, then #add\_periodic\_timer is the right solution.  
#add\_periodic\_timer (or #add\_timer) is client-specific because you pass  
it a  
block, which is of course a closure, so it has access to your local  
scope.  
The following code will do what you want. Try it with multiple  
connections  
and see:

def receive\_data data  
local\_scope\_data = “whatever\n”  
EventMachine.add\_periodic\_timer(2) {  
send\_data( local\_scope\_data )  
}  
end

If your async data comes from other events in the system (like calls to  
web-services) then you can use EventMachine’s Deferrable pattern, which  
works much like the one in Twisted, but easier to use because it’s Ruby  
:-).  
If you want to do that, feel free to write me offlist and I’ll show you  
how  
to use it.

Unrelated comment: I’m finding that a lot of people have a need to  
accept  
network connections, receive data from remote clients, and then respond  
with  
data that is aggregated from other network services (databases, other  
web  
sites, etc). This is exactly the kind of pattern EventMachine is good  
for.  
I’m thinking about adding some specific API support tosimplify this  
pattern,  
especially for use within Rails apps (where your controller may need to  
get  
data from one or more outside sources to send back to your client). Is  
this  
of interest to anyone?
