# Simultaneous HTTP connect with EventMachine

**URL:** <https://www.ruby-forum.com/t/simultaneous-http-connect-with-eventmachine/203024>\
**Category:** Ruby\
**Created:** [February 10, 2011, 8:03pm UTC](https://www.ruby-forum.com/t/simultaneous-http-connect-with-eventmachine/203024 "2011-02-10T20:03:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 10, 2011, 8:03pm UTC](https://www.ruby-forum.com/t/simultaneous-http-connect-with-eventmachine/203024/1 "2011-02-10T20:03:50Z")

</div>

Can anyone give many any tips about how to have multiple  
EventMachine.connect instances running at the same time? I thought  
em-synchrony looked promising, but it only works with  
EventMachine::HttpRequest.

I need EventMachine.connect because my application streams http data  
using the receive\_data callback, which EventMachine::HttpRequest lacks.

If I try running more than one streaming EventMachine.connect normally  
inside EM.run, I only see the results of the last EventMachine.connect.  
(even if I run this in a separate thread still only shows the results of  
the last .connect)

help?

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [February 10, 2011, 9:21pm UTC](https://www.ruby-forum.com/t/simultaneous-http-connect-with-eventmachine/203024/2 "2011-02-10T21:21:49Z")

</div>

On Thu, Feb 10, 2011 at 12:03 PM, Shea B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone give many any tips about how to have multiple  
> EventMachine.connect instances running at the same time? I thought  
> em-synchrony looked promising, but it only works with  
> EventMachine::HttpRequest.
> 
> I need EventMachine.connect because my application streams http data  
> using the receive\_data callback, which EventMachine::HttpRequest lacks.
> 
> If I try running more than one streaming EventMachine.connect normally  
> inside EM.run, I only see the results of the last EventMachine.connect

You are doing something wrong.

EM.connect, just like everything else in EM, is asynchronous. You can  
connect to many places at the same time. I’ve done tests with 20000  
connections before.

As for receive\_data, all connection handlers have a receive\_data,  
since that is what EM calls when it receives a chunk of data. If the  
handler descends from EventMachine::Connection, it will have a default  
receive\_data defined for it.

However:

> <https://github.com/eventmachine/em-http-request/blob/master/lib/em-http/client.rb#L226>

You need to provide more details about what you are doing before  
anyone is going to be able to tell you what you are doing wrong.

Kirk H.  
Software Developer  
Engine Y.

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [February 10, 2011, 10:30pm UTC](https://www.ruby-forum.com/t/simultaneous-http-connect-with-eventmachine/203024/3 "2011-02-10T22:30:37Z")

</div>

More details:

Im trying to use Twitter::JSONStream, which is an  
EventMachine::Connection to simultaneously watch two different twitter  
filters using the streaming API.

gem ‘twitter-stream’

require ‘twitter/json\_stream’  
EM.run do  
a = Twitter::JSONStream.connect :path =\>  
“/1/statuses/filter.json?track=:)”, :auth =\> “username:password”  
b = Twitter::JSONStream.connect :path =\>  
“/1/statuses/filter.json?track=:(”, :auth =\> “username:password”

a.each\_item {puts “happy” }  
b.each\_item {puts “unhappy” }

end

This should be printing alternating happy and unhappy, but right now it  
only prints one of the two

This is my first time actually using EventMachine. thanks for the help.
