How do you spawn an EventMachine “inside” a Rails app?

前端 未结 8 1940
星月不相逢
星月不相逢 2020-12-13 14:54

I\'ve got a Rails application, and am looking to add some sort of WebSocket support to it. From various googling, it appears that the best Ruby based WebSocket solution is

相关标签:
8条回答
  • 2020-12-13 15:14

    Don't know if this is what you are after. But if you would like to do provide some kind of socket-messaging system.

    Have a look at Faye. It provides message servers for Node.js and Rack. There is also a rails cast for this by Ryan Bates which should simplify the implementation.

    Hope that helps.

    0 讨论(0)
  • 2020-12-13 15:16

    I'd try using em-synchrony to start a reactor in a fiber. In a rails app you can probably start it in an initializer since it sounds like you just want to leave the reactor running to respond to websocket requests. As suggested by the other answers I think you want to either setup socket communication with your reactor or use one of the asynchronous clients to a data store which both your reactor and rails code can read from and write to to exchange data.

    Some of my coworkers put together some examples of starting EM reactors on demand in ruby code to run their tests within EventMachine. I'd try using that as a possible example; raking and testing with eventmachine

    0 讨论(0)
  • 2020-12-13 15:24

    If you run rails application in a thin server (bundle exec thin start) thin server run EventMachine for you and then your rails application can execute EM code wherever you need.

    By example:

    A library o initializer with that code:

    EM.next_tick do
      EM.add_periodic_timer(20) do
        puts 'from Event Machine in rails code'
      end
    end
    

    not blocks rails processes application.

    0 讨论(0)
  • 2020-12-13 15:27

    You probably shouldn't use EM any more if you can help it, it seems to no longer be maintained - if you encounter a bug - you're on your own.

    Most of the answers above don't work in JRuby due to https://github.com/eventmachine/eventmachine/issues/479 - namely the pattern:

    Thread.new{ EM.run }
    

    used by EM::Synchrony and various answers found around the internet (such as EventMachine and Ruby Threads - what's really going on here?) are broken under JRuby eventmachine implementation (fibers are threads in jruby and there's currently no roadmap on when this will change).

    JRuby messaging options would be

    1. deploy with TorqueBox (which comes bundled with HornetQ), http://torquebox.org/news/2011/08/15/websockets-stomp-and-torquebox/, impressive and enterprisey but not really elegant unless you're coming from a Java background
    2. newer versions of Faye should work with JRuby, Faye in jruby on rails
    3. note for the future, keep an eye on the celluloid community, some interesting distributed solutions are coming from there https://github.com/celluloid/reel/wiki/WebSockets, https://github.com/celluloid/dcell
    4. ?
    0 讨论(0)
  • 2020-12-13 15:32

    I'd consider looking into Cramp. It's an async framework built on top of EventMachine, and it supports Thin server:

    Rack Middlewares support + Rainbows! and Thin web servers

    0 讨论(0)
  • 2020-12-13 15:38

    I spent a considerable amount of time looking into this. EventMachine need to run as a thread in your rails install (unless you are using Thin,) and there are some special considerations for Passenger. I wrote our implementation up here: http://www.hiringthing.com/2011/11/04/eventmachine-with-rails.html

    UPDATE

    We pulled this configuration out into a gem called Momentarily. Source is here https://github.com/eatenbyagrue/momentarily

    0 讨论(0)
提交回复
热议问题