imanel / socky_gem

Socky is a WebSocket server and client for Ruby on Rails
http://socky.org
72 stars 1 forks source link

Synchronous callbacks? #1

Closed rsbrown closed 13 years ago

rsbrown commented 14 years ago

First off -- I love what you've done with Socky. Excellent work!

Is it possible to create synchronous callbacks for my Rails app from Socky? Whenever the Socky gem receives a message, I want to process that message using a controller action, but without introducing the overhead of an http call between Socky and Rails. Ideally, this would be a config option like the subscribe/unsubscribe URLs currently supported, but a controller method. For example:

:message_action: SockyController.receive_message

Socky currently seems geared towards "push" style apps (i.e., broadcasting messages out to all clients), but I have a need for fully synchronous pull/push functionality, similar to what lifo has done with Cramp.

Is this possible with Socky?

imanel commented 14 years ago

Well... Currently it is not possible because of simple thing - Socky is standalone and have no connection to web-server process(that's why subscribe/unsubscribe is URL instead of method) But I plan to move Socky away from em-websockets and use Thin as server so it will be possible in near future. Meower I don't think that it is good thing if you will have a lot of users. Thousands of active connections + normal server requests will kill your server(that's why Cramp have very poor performance on heavy load sites)

rsbrown commented 14 years ago

This makes sense. I don't mind running two separate servers (one for http and one for websockets), but I would like to be able to utilize my Rails models for processing realtime messages from the websocket clients.

Would it be possible to bind a new type of controller class to the Socky server for processing realtime requests? This controller would be outside the scope of the Rails HTTP server, but still have access to the Rails model objects.

imanel commented 14 years ago

Right now I propose to make small hack for yourself. Simplest way should be adding command line call to some background drb which have Rails models inside. If you searching best place it will probably be Socky::Message#send_message but remember to make it unblockable - for example by using EM.add_timer

class Socky::Message
  def send_message(message, connections)
    super
    EM.add_timer(0.1) do
      `<drb_command>`
    end
  end
end
rsbrown commented 14 years ago

Sounds good. Many thanks, Bernard.

imanel commented 13 years ago

Closing