-
Notifications
You must be signed in to change notification settings - Fork 226
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
Integration with frameworks #70
Comments
Good question. I think we already have (or at least had) a similar question. I do agree that the integration with web frameworks is very desirable and we would also like to be able to use such things in production. There are however some frameworks which were able to make a nice integration. One such example is warp. They use My idea was to analyse that part inside But that's all is more about elimination of boilerplate and copy-paste part inside the frameworks (currently |
Yes, that was exactly my question, whether I took a look at what So I think the minimal API looks fairly simple: struct WebSocketReq { /* key, version, requested extensions, and so on */ }
/// Extract upgrade information from HTTP request.
fn prepare(req: http::Request) -> Result<WebSocketReq, Error>;
/// Generate `101` response from the request.
fn accept(ws_req: WebSocketReq) -> http::Response; I think this can serve both Do you think this direction is worth pursuing? |
This issue has come up in tide: http-rs/tide#67 as to how to support websockets there. I see the situation as follows: We have at least 5 implementations of phase 1: actix-web, hyper, tungstenite, ws, websocket, + a number of forks of some of those. We have at least 5 implementations of the second phase (hyper doesn't implement that but warp does). The problems this implies:
Those problems are present for both phases. Ideally there would be 1 very good implementation of websockets in rust. It's always hard to get a bunch of people with different visions to work together, but I think it's safe to say the current situation is unfortunate. What is particular to the first phase is that it also concerns http servers, and not only the other three libs. Maybe it would be good start with this first phase as an attempt to both reduce duplication of effort and improve quality. I think it would be a good idea to split it in a separate crate, co-developed by the authors of all four current implementations. I'm in an uncomfortable position proposing this, since I'm not one of such authors and I don't want to be, but I really think it would be the way forward. If there were 1 websocket_handshake crate that everybody could agree on, it would be a great day for rust networking! Just want to mention about warp, that they don't want to expose tungstenite (implementation detail), and thus have provided their own websocket API (bye bye interoperability), which has numerous issues which i have started to describing here: seanmonstar/warp#257 and seanmonstar/warp#258. If there was only one reference websocket crate in rust that everyone agreed was good, maybe they could agree to use that instead of rolling their own. |
I would also like to change |
Together with #93, I think the only thing missing for easy integration with async frameworks would be to move Once that is there, All that is already possible with tokio/async-tungstenite (once they're also updated to use the Anything I'm missing here? |
I really hope that they'll release a stable version soon which will rely on the same traits. As far as I understood it, this was the only one (?or maybe one among several reasons?) why the PR which brings the support of async to tokio-tungstenite uses |
They intend to stay with their own versions of the traits for the time being, see tokio-rs/tokio#1297
The unsafe code is not needed from what I can see, but I'll spend time on removing that once it's actually otherwise correct (see snapview/tokio-tungstenite#68 (comment) ). The |
I would like to be able to use tungstenite to add websocket support to an existing web-app, using the same tcp listener for both WS and regular HTTP requests.
This means that both web-server frameworks and WS libraries need to be able to inter-operate. Some pieces are already in place:
hyper
, for example, supports HTTP upgrade in a generic way, which allows servers to perform websocket handshake; andtokio-tungstenite
providesWebSocketStream::from_raw_socket
, which can take over after the handshake. An example of this can be found in gotham-rs/gotham#354.The missing bit is the handshake itself.
tungstenite
does HTTP parsing itself from the byte streams, something that can not be used in a framework based app: once the route handler is invoked, the request was already parsed; the response typically must come as some high-level 'Response` object too, rather than a stream of bytes.So the question is, would it make sense to extend tungstenite to support this use case? Ideally, user would supply request headers in a high-level form, and receive result, indicating whether websocket connection can be initiated and what the response should be.
The text was updated successfully, but these errors were encountered: