kotovalexarian / lita-gitter

Gitter adapter for the Lita chat bot.
http://lita.io
MIT License
2 stars 3 forks source link

Support multiple rooms #18

Open ghost opened 9 years ago

brodock commented 9 years ago

I have an idea on how to implement this in some simple incremental steps:

  1. use eventmachine to subscribe to multiple stream api endpoints: https://developer.gitter.im/docs/streaming-api
  2. automatically subscribe to endpoints listed here: https://developer.gitter.im/docs/rooms-resource#list-rooms
  3. in a future version, listen to events from faye endpoints: https://developer.gitter.im/docs/faye-endpoint

will give a try to the first point and report back when have it working

ghost commented 9 years ago

Can you implement the first point? I don't fully understand how to do it with EventMachine.

brodock commented 9 years ago

Yes, sorry for the delay... I will have enough time to contribute on the weekend.

I'm not 100% sure how to do, but I believe will be something based on faye channel subscription: http://faye.jcoglan.com/ruby/clients.html, with the exception that for every "subscribed channel", instead of creating a loop or callback, will spawn/delegate using EM.defer to an EventLoop like the one present on RTMConnection: https://github.com/kenjij/lita-slack/blob/master/lib/lita/adapters/slack/rtm_connection.rb#L39

Does it make sense?

Eventmachine is an hybrid reactor-pattern implementation. While the main thread runs evented loop, you can spawn additional "worker threads" to perform different computations using EM.defer.

It would be really cool if @jimmycuadra could give a feedback here :)

ghost commented 9 years ago

I have tried to subscribe for multiple channels with Faye, but with no result.

Is multithreading really necessary for this? I prefer to avoid it.

brodock commented 9 years ago

Lita already uses it (threads).

I have to read documentation again to fully understand whether defer is the right thing or how to use eventmachine own eventloop code correctly (I believe we will have to use something like Fibers).

The whole idea is that we need something to subscribe in an "evented way" to different endpoints and when it receives valid data, it should delegate tho the handlers in a non blocking way (this will use defer).

This last step is important because handlers can do heavy operations like remote IO, etc. If we don't isolate the handler from the eventloop, lita will have huge latency in crowded channels.