Friday, November 5, 2010

Embedding Cramp in a Rails App, Part II

After my last post on getting "Hello World" running on Cramp alongside a full rails application, I thought I'd continue to catalogue my progress on actually getting a websockets action running and doing a real data push back to the client.

First things first, the blog post by Pratik that introduces the concept is invaluable: http://m.onkey.org/2010/1/15/websockets-made-easy-with-cramp.

Following the directions there, I created an initializer (config/initializers/cramp_server.rb for cramp and included the following:
Cramp::Websocket.backend = :thin

Notice that this isn't quite the same as the blog post, because I'm using edge cramp, and it's been refactored to remove the "Controller" module.

Anyway, rebooting the server everything still worked, so the next step was to rebuild my controller as a websockets aware rack application.

class ChatAction < Cramp::Websocket
  on_data :received_data

  def received_data(data)
    render "Got your #{data}"
  end
end

Which did indeed startup and respond to data requests from the client with an echo. Nice!

However, in order to support IE users, it would be necessary to use flash sockets, and although it's not that hard to do, I'm already getting uncomfortable with the number of things that are new to me in this app. So, as a responsible design decision, I'm planning on just using Cramp for long-polling for the time being until better websockets support is available across all the major browsers.

Thanks Pratik for such a cool framework!

No comments: