Show HN: Realtime Server-Sent Events in Go
sse.getgin.io
sse.getgin.io
```
var int = 1;
window.setInterval(function(){
int = int + 1; var data = { message: "test " + int, nick: "rl" };
$.ajax({ type: "POST", url: "http://sse.getgin.io/room-post/hn?nick=rl", data: data, });
}, 5);
Below is my stab from a while ago. It's more of an example than a library.
https://github.com/wyc/go-server-sent-events-example/blob/ma...
I think it's great that the language makes the implementations so similar.
I've used SSE in the context where we sent chunked pages of one output stream (where the stream was generated server-side all at once), but not where the connection is opened and then actually waiting for additional server information. Is it common for SSE connections to be held open for 30 seconds or even several minutes to be able to stream information?
So if your SSE connection is 10 minutes long and you might have more than 64,000 simultaneous connections to your vanilla box in those 10 minutes, you just need to start load-balancing I guess.
Of course you'll be limited by other factors such as kernel fd limits (which are adjustable) and the CPU needed to monitor lots of sockets.
At least in Firefox, the limit for websockets is much more generous (200, but shared across the whole browser, IIRC).
Both the Chrome and Firefox dev teams WONTFIXed my suggestion to increase the limit for Server Sent Event connections.
Chat, however, would be better served with websockets. If it's bidirectional, use websockets and avoid both upstream and downstream cost of setting up a connection.
SSE works, but you should consider it tech debt if you decide to go that way.
</psa>
Here's a recent article about Secret using POST for sending: https://medium.com/@davidbyttow/scaling-secret-real-time-cha...
At scale, you might be talking hundreds or even thousands more clients per server (depending on the server size). On mobile, you use less data which fires up the radio less which directly translates to battery life.
Assuming that your application gets any sort of traction, the 30 minutes of dev time you saved may have a much higher net impact on the world purely by the multiplicative effect. When your product affects that many people, you should remember that you have a responsibility to be sustainable.
It doesn't use Gin, but httprouter, which is also the basis for Gin.
Thanks for providing the link - I'll definitely learn me some httprouter soon ..
It is a very pretty demo tho, well done :)
Edit: actually this looks to be a lot more sophisticated than the other shims i havr looked at and does indeed behave more like eventsource. Good job!
Addition: Verified. It doesn't disconnect between events (not long-polling).
EventSource natively supports this via the "Last-Event-ID" header (and can also be used by a shim to ensure it gets messages it missed). And in the case of a room style app you probably already have a room history that can be used to provide this data.
However, why would I want to use a shim over websockets?
tl;dr websockets can fail in strange ways on mobile and when passed through proxies and corporate firewalls