-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Support for Server Sent Events #33
Comments
Unless otherwise stated, everything is streaming in Rocket by default, not just On a more fundamental level: why would someone use server-sent events instead of web sockets? I think seeing some kind of native support for web sockets would be nice, and it would be even nicer if that supplanted the need for server-sent events. |
Because if you only need unidirectional (server to browser) notifications, why set up the more complex WebSocket protocol? Okay, I know WS is not actually complex, but still. SSE just feels lighter. |
+1 on the native support for web sockets within Rocket |
@SergioBenitez There are a number of differences between Server Send Events (SSE) and WebSockets (WS) -- definitely worth reading up on. They both have value for their specific use-cases. SSE has some great features, and WS has some problem that only become apparent once you investigate them. For example, WS don't currently work with AWS ELBs unless the ELB is in PROXY-mode, which removes a lot of the benefit of an ELB! Also, the security model for WS is different than standard HTTP, which complicates securely implementing WS. WS need an UPGRADE on the HTTP protocol to work, whereas SSE does not. There are a number of other advantages to SSE (easy to implement, browsers automatically reconnect if they become disconnected, ... more ... more), and are good for when you don't have to have the bi-directional capabilities or super-low-latency of WS. I would definitely vote +1 for SSE support in Rocket (which, I'm still loving!) |
This would be great. I tried to make my own implementation using a Responder but my rust knowledge is not there yet. I would love to see an example of how to do SSE in Rocket. |
I believe Rocket should be able to handle Server Sent Events. It's likely that the implementation would reside in |
Hello, i am having (almost) the same need. As i am a complete beginner with rust (im trying to see how hard it would be to port an existing project from C to Rust), but i still have been able to come up with this code : What it does is simply read on a unix pipe. So i create a PipeLoop structure, implement Still, there is a thing that doesnt work properlly : rocket.rs will bufferise on its side. For example, if my pipe is
Only to receive "toto" on the client side. This is where i begin to not know what to do. I'd like to come up with a workaround before v0.8.0 (having a long running thread isn't a problem in my case). |
Using This makes me believe (i am not yet able to determine it) that hyper will bufferize on its side. |
Using Here is a code example : https://gist.github.com/gfriloux/c8846afed6d2bfceffd13fc5375bced3 |
I dont know where its blocking (my low rust knowledge doesnt help) but i assume it should not wait to have the buffer filled to send data. Data should be sent either immediately or after a very short window. Comparing to C functions, |
Ok, after spending some days on other stuff, im back at this. setting logs at debug level, i can see that hyper writes the data, but the HTTP client doesnt get it :
You need to write a bunch of data for it to be received by the HTTP client (it wont even send the HTTP headers, in some cases). So i will stop reporting here this behavior. |
Ok, I managed to patch version 0.10.13 of hyper to force flush sockets. Basically, for hyper 0.10.13, edit
This will properlly flush the socket and make it possible to send only a few bytes. |
In my opinion it would be great to have an example that shows how to currently implement a simple SSE or websocket solution using the current API (I guess using Stream in some way). |
The problem with an example of SSE in the current API is related to this point:
"will block" has the serious consequence that the maximum number of clients with open SSE channels is bounded by the number of worker threads, at which point the server will "hang" until those clients disconnect. If I'm reading #33 (comment) correctly, then hyper also needs modifications for SSE to work correctly. #1065 will make doing this not-dangerous - open |
I think my MR #1365 would satisfy this request for Rocket 0.4.x. Having read the discussion, I wanted to respond to a few points:
Yes. Depending on the application, it will be necessary to significantly increase the number of worker threads (and hope the system doesn't overload or get DOS'd). Also there is a browser limitation, see https://developer.mozilla.org/en-US/docs/Web/API/EventSource for which traditional workaround is to make the SSE requests to a wildcard domain with a randomly-chosen leafname, or similar (the client maximum connections limit being per domain, not per IP address).
I don't think that change is necessary (everything works for me with hyper 0.10.16) or desirable. If rocket can be persuaded to complete a chunk, and issue a flush call, hyper seems to do the right things.
I have code that could easily become such a thing if my MR #1365 is accepted. If there is interest I could produce a suitable git branch. |
Pushing this up to |
I'm, sadly, going to push this to 0.6 to clear the way for 0.5. It's likely we can have a solution as a point release in the 0.5 series, however. |
newb here. I see this is a long running thread, so unsure if this is the right place for these comments. I've run the sse demo in As of Mar/2021 how do the proposal above change the behavior or API in the demo?
|
@lpgeiger You're mixing documentation/examples, and perhaps code, from In general, "nice" SSE support is blocked on a write-based API for responses. We can emulate such an API in Rocket with a performance cost, but it sub-optimal. Ideally, an underlying HTTP library would support such an API, but that is not the case today.
This example works in both |
Reading up on this very thing, http2 can basically do websockets with fetch api and sse. |
Bringing this back to 0.5, thanks to @jebrosen's nearly complete implementation! |
Support has landed! See the |
It looks like the streams could be used to implement Server Sent Events, but it would be nice if there was some built in support.
The text was updated successfully, but these errors were encountered: