News for hackers. Stuff that matters.
Re:Server-sent events, WebSockets, and HTTP by ShoveItHN on Sunday February 20, 2022 @02:57AM (#30404056)
Interesting. I'm writing an application that will control devices over HTTP with a REST-style API (defined in OpenAPI). But we also want those devices to be able to alert the controlling application to events without being polled. This won't be a large datastream, but rather the occasional update of progress or notification of a state change.

I was considering Websockets or MQTT (or both), so server-sent events sound like a direct and possibly superior competitor. But from a design standpoint, what do you do? Just do a GET on some general "status" endpoint to open this SSE stream and then have the server send everything down this pipe indefinitely?

Re:Server-sent events, WebSockets, and HTTP by samwillis on Sunday February 20, 2022 @03:09AM (#30404125)
Yes, it’s just a GET that stays open, super simple. There are various client libs that support SSE outside of browsers, worth having a look if there is one for the language you are using.
Re:Server-sent events, WebSockets, and HTTP by ShoveItHN on Sunday February 20, 2022 @03:24AM (#30404187)
Thanks! I haven't done much network programming.

While this SSE channel stays open, the controlling app will need to be able to continue to do additional traditional GET, POST, etc. calls to the server. How are responses to those distinguished from data coming in from SSE?

Re:Server-sent events, WebSockets, and HTTP by drothlis on Sunday February 20, 2022 @04:45AM (#30404508)
Same way that if you make 2 "normal" GETs simultaneously to the same server the responses don't get mixed up, i.e. they'll be separate HTTP Connections which are separate TCP connections:

> Each HTTP connection maps to one underlying transport connection.

-- https://httpwg.org/http-core/draft-ietf-httpbis-messaging-la...

TCP uses "port numbers" to identify different connections. 2 different connections from your PC to the same web server will use 2 different "source" ports. A port is just a number in the TCP header. https://en.wikipedia.org/wiki/Transmission_Control_Protocol#...

HTTP/1.1 added pipelining, so you can make several requests on the same TCP connection before receiving a response. But the (complete) responses must arrive in the same order of the requests so it doesn't work for SSE.

HTTP/2 added request & response multiplexing on the same TCP connection. But (according to the OP) there are some limitations that affect SSE.

Re:Server-sent events, WebSockets, and HTTP by eldelshell on Sunday February 20, 2022 @04:05AM (#30404331)
SSE has its own API (EventSource), it's not a normal fetch/ajax request.

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent...

Re:Server-sent events, WebSockets, and HTTP by toomim on Sunday February 20, 2022 @05:21AM (#30404671)
Your use-case sounds perfect for Braid: https://braid.org

This works like SSE, but is designed specifically to articulate changes to the state of HTTP/REST resources.

If you're in Javascript, you can use the braidify library: https://www.npmjs.com/package/braidify

Re:Server-sent events, WebSockets, and HTTP by flyinprogrammer on Sunday February 20, 2022 @09:17AM (#30405868)
You might also checkout https://rsocket.io/