Luis
Luis

Reputation: 41

How to run sinatra as a websocket server?

I've tried using eventmachine but all I have is an application running sinatra on one side and an event machine server on the other. I can't figure out how to run a sinatra application as a websocket.

For example, I want to serve this as a websocket:

get '/' do
   #pull info from DB and display
end

Any help is appreciated.

Thanks! L.

Upvotes: 4

Views: 4379

Answers (3)

red-o-alf
red-o-alf

Reputation: 3245

Did you check "sinatra-websocket" ?

From the maintainer:

[...] It let's you run the websocket server in the same process and on the same port as Sinatra. [...]

Upvotes: 5

Jakub
Jakub

Reputation: 739

I've been down this road but it is not something I would recommend. It is possible to have the same Rack stack handling both Sinatra and Websocket somehow, using Thin, but there are more problems than benefits. If you want to see how I did it, look at:

https://github.com/HakubJozak/mana/blob/before-sinatra-removal/config.ru

Now I am actually porting the app to use Rails 3.1 for "static" pages and brain simple quick backend based on github.com/igrigorik/em-websocket.

Upvotes: 0

Daniel
Daniel

Reputation: 1

From what I understand doing this would require writing an extension or possibly modifying Sinatra it's self to use web-sockets instead of http.

I would write an extension that routes the request through a EM websocket server running in side the extension if I use ws.get or ws.post insted if just get or post

Upvotes: 0

Related Questions