Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Blather Clients Not Working #129

Open
etm opened this issue Mar 25, 2014 · 3 comments
Open

Multiple Blather Clients Not Working #129

etm opened this issue Mar 25, 2014 · 3 comments

Comments

@etm
Copy link

etm commented Mar 25, 2014

When multiple blather clients are created in a program only the first one works. All other clients can't connect (never get ready).

2.times do
Thread.new do
EM.run do
client = Blather::Client.setup ...
client.register_handler(:ready) { print ... }
client.connect
end
end
end

@benlangfeld
Copy link
Member

You have to run all of them in the same EM reactor for now. I have no
interest in fixing all of EM's nonsense, and much prefer to push forward
with the move to Celluloid. Contributions welcome if you do have a fix for
this, though :)

On 25 March 2014 16:20, eTM [email protected] wrote:

When multiple blather clients are created in a program only the first one
works. All other clients can't connect (never get ready).

2.times do
Thread.new do
EM.run do
client = Blather::Client.setup ...
client.register_handler(:ready) { print ... }
client.connect
end
end
end

Reply to this email directly or view it on GitHubhttps://github.com//issues/129
.

@etm
Copy link
Author

etm commented Mar 25, 2014

Hmm, solved it in my code by working around with EM#defer. This is a generic quick & dirty solution for most EM thread problems :-)

2.times do
  Thread.new do
    EM.send EM.reactor_running? ? :defer : :run do
      client = Blather::Client.setup ...
      client.register_handler(:ready) { print ... }
      client.connect
    end
  end
end

But after some quick digging, I think i know what the problem is :-)
It's in lib/blather/stream.rb Stream#self.connect. Please see https://github.com/eventmachine/eventmachine/wiki/FAQ -> "Does EM work with other Ruby threads running?"

When changing self.connect you lose the rescue NoConnection, but you gain proper support for multiple clients.

def self.connect(host, port, conn, client, jid, pass, connect_timeout = nil)
  EM.next_tick do
    EM.connect host, port, conn, client, jid, pass, connect_timeout
  end
end

As users are losing immediate connection problem handling (how many even check?, probably none :-)), something like a :connection_error handler similar to :ready handler would be nice. But one can survive without it - EM#error_handler should catch it too. Maybe an example in the README would suffice.

@ThomasSevestre
Copy link

In case somebody is looking for multiple client support. Here is a simple solution :

EM.run do
  2.times do
    client = Blather::Client.setup ...
    client.register_handler(:ready) { print ... }
    client.run
  end
end

A few patches are needed in blather :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants