Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ring handler for Websockets #41

Closed
yogthos opened this issue Aug 5, 2019 · 9 comments
Closed

Ring handler for Websockets #41

yogthos opened this issue Aug 5, 2019 · 9 comments

Comments

@yogthos
Copy link
Contributor

yogthos commented Aug 5, 2019

Are there any plans to support a standard Ring handler for Websockets as seen in Immutant and HTTP Kit. For example, something along the lines of:

(defn handler [request]
  (with-channel request channel
    {:on-connect (fn [ws])
     :on-error (fn [ws e])
     :on-close (fn [ws status-code reason])
     :on-text (fn [ws text-message])
     :on-bytes (fn [ws bytes offset len])}))

This would allow ws routes to be defined the same way regular routes are defined via standard routing libraries. The ws routes would then be able to share all the middleware such as sessions and authentications with the other routes in the application.

@sunng87
Copy link
Owner

sunng87 commented Aug 7, 2019

For now WebSocket paths were defined separately because Jetty's API uses different handler for WebSocket and regular HTTP servlet requests. I'll look into other implementation to see possible solution.

@yogthos
Copy link
Contributor Author

yogthos commented Aug 7, 2019

One idea might be to just chain the handlers internally. It looks like it's possible to update the HandlerList dynamically, so perhaps something like this would be called on an incoming Ws request.

In the example above with-channel macro could handle Ws negtiation as needed and then call the body within the Ws context.

@robert-stuttaford
Copy link
Contributor

+1 for this. It seems there is no way to use dynamic routing for WebSockets under the current implementation?

@yogthos
Copy link
Contributor Author

yogthos commented Apr 30, 2020

I actually ended up making an implementation of that here.

@sunng87
Copy link
Owner

sunng87 commented Apr 30, 2020

Ring 2.0 seems to have such a section for websockets https://github.com/ring-clojure/ring/blob/2.0/SPEC-2.md#3-websockets

@yogthos
Copy link
Contributor Author

yogthos commented Apr 30, 2020

yeah, looks like that's going to be part of the spec going forward

@awb99
Copy link

awb99 commented May 17, 2020

It wuld be really great if jetty couldhave the same api for websockets as httpkit, ...
Otherwise one can not switch between different webservers.
Also this would make it much easier to add jetty support for sente

@sunng87
Copy link
Owner

sunng87 commented May 18, 2020

Actually I think you can use the same ring app for both main app and websocket path, too. Just keep in mind your websocket path in the route should be consistent with the path specified in (run-jetty)

(defroute app
  (GET "/" [] {:status 200})
  (ANY "/path1" [req]
    {:on-message (fn [ch text] (send! ch text))}))

(run-jetty app {:port 8080 :websockets {"/path1" app}})

@sunng87
Copy link
Owner

sunng87 commented Nov 1, 2021

This is supported via #59 finally.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants