Puma: Please join the Rack Specification discussion for `env['upgrade.websocket']`

Created on 6 Aug 2016  路  2Comments  路  Source: puma/puma

I represent an effort to extend Rack so that it allows server-side websocket upgrade implementation support and pure Rack websocket applications.

This new Rack feature proposal is gaining support, with over 42 support votes in the original Rack discussion thread.

You may have read my blog post about this or the reddit discussion.

This proposal simplifies Websocket applications by leaving all the network complexity were it is (with the application's web server), allowing application programmers to focus on their application logic.

Using the proposed specification, a pure Rack Websocket echo server could be written as simply as:

# this is a toy example.
class MyEcho
   def initialize(env = nil)
      # optional initialization
   end
   def on_message(data)
     write "Echo: #{data}"
   end
end

app = Proc.new do |env|
   if env['upgrade.websocket?'] && env['HTTP_UPGRADE'] =~ /websocket/i
      env['upgrade.websocket'] = MyEcho.new(env) # or simply `MyEcho`
      [ 0, {'X-Header': 'data'}, [] ]
   else
      [200, { 'Content-Length' => '12' }, ['He11o World!']]
   end
end

run app

There's a working Gist chatroom example.

I invite you to join the discussion and help shape the proposed specification.

Most helpful comment

I'll leave some comments here as well as on the Rack issue. I'm all for adding support for this, mostly because currently the websocket support is wired in a sort of weird way for puma anyway. Puma already has all the infrastructure to support evented websocket support so I'm all for it.

In fact, I might spike an implementation shortly.

All 2 comments

@evanphx do you think you can support the rack.websocket effort?

I'll leave some comments here as well as on the Rack issue. I'm all for adding support for this, mostly because currently the websocket support is wired in a sort of weird way for puma anyway. Puma already has all the infrastructure to support evented websocket support so I'm all for it.

In fact, I might spike an implementation shortly.

Was this page helpful?
0 / 5 - 0 ratings