Skip to content

Upgrading connection to WebSocket (perhaps using underlying hyper::Request ?) #1703

New issue

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

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

Already on GitHub? Sign in to your account

Closed
ErwanDL opened this issue Jun 15, 2021 · 2 comments
Closed
Labels
declined A declined request or suggestion request Request for new functionality

Comments

@ErwanDL
Copy link

ErwanDL commented Jun 15, 2021

Hello, and first of all thank you for the great work on Rocket.

I've been following the discussion about native support for WebSockets on #90, and as far as I understood support for WebSockets has not been integrated into the Rocket API yet (I'm using v0.5-rc1). Is there any "hack" or manual solution that you know of for upgrading an opening handshake to an actual WebSocket connection in Rocket ?

While looking for a solution, I noticed that hyper supports upgrading an HTTP connection via the hyper::upgrade API (in hyper v0.14, this is done using the upgrade::on function). But in order to use that API, I believe I need access to a hyper::Request object to call upgrade::on() on. Is there any way to access the underlying hyper::Request object in a Rocket handler, or to convert a rocket::Request to a hyper::Request by any means ?

@SergioBenitez SergioBenitez added the request Request for new functionality label Jun 15, 2021
@SergioBenitez
Copy link
Member

Unfortunately, there isn't. I've oft contemplated whether Rocket should expose such a mechanism, for this reason and others, and time and time again, my conclusion is the same: no. Rocket strives to, above anything else, make it very difficult, ideally impossible, to make security and correctness mistakes. But exposing such a raw mechanism is fraught with pitfalls of every kind: security, correctness, performance, and usability. For this reason, we don't implement features unless we have a well thought out plan. Our support for web sockets will be the same.

Thankfully, @the10thWiz has been making stellar progress towards such an implementation (#1639). If you want to follow along with development discussions, join us on Matrix.

In the meantime, if you absolutely need websockets because, for instance, you need to respond to websocket requests from an external provider, then Rocket is unfortunately not an option at the moment. You can still use Rocket for non-websocket HTTP requests and run a websocket server on a separate port using another websocket library, however.

If you, however, you simply need real-time communication, then perhaps Server Sent Events are a good option. See the chat example as a fully developed application that makes use of SSE to implement a real-time, multi-room chat application.

@SergioBenitez SergioBenitez added the declined A declined request or suggestion label Jun 15, 2021
@ErwanDL
Copy link
Author

ErwanDL commented Jun 15, 2021

Thank you for the answer. I'll be watching closely the evolution of the implementation on #1639. Unfortunately I cannot use SSE as this is an in-production project which already uses Websockets. I think I will stick with a Hyper-only solution for now, but I am looking forward to see this being released in Rocket. Keep up the great work !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
declined A declined request or suggestion request Request for new functionality
Projects
None yet
Development

No branches or pull requests

2 participants