5 ms·
Do you mean Server-Sent Events? That's a well-supported and efficient model that'll work fine, although the payload will always be heavier than websockets since
by manigandham 9y ago
Do you mean Server-Sent Events? That's a well-supported and efficient model that'll work fine, although the payload will always be heavier than websockets since all the headers have to be sent, even if compressed within the HTTP/2 stream. It's also not supported on IE or Edge.
If you actually mean server-push then that would basically be long-polling and not very efficient, also it's meant to send along assets that the server already knows will be needed for a request (like CSS with the HTML). There's not much client-side API to actually deal with server-push because it's just like another network http request/response.
- rcarmo 9y agoI’m sorry, but when you mention all headers need to be sent, what are you referring to? I have working SSE implementations where I only send out the headers once and then pipe minimal data to the client while the socket is open.
- manigandham 9y agoYou're right, I misspoke. I meant that the "data: " pseudo-header for each SSE message is always included, and repeated for multiple lines (but it's HTTP so can be gzipped). It's also UTF8 encoded and meant for text while websockets can send the exact binary bytes you need and no more. Either way, it doesn't really matter unless it's a latency-sensitive or high-volume service, but I prefer the simplicity of SSE.
- invisible 9y agoYeah, but packets being able to be maximized to the window size of the connection early on (and being able to have more than 6 "parallel" downloads as a result) is great for initial page loads.
- nailer 9y agoWe use SSE for realtime identity verification updates at CertSimple but note it's hampered by lack of support in Edge - looking at Microsoft's roadmap it doesn't look like it's coming soon either.
- Touche 9y agoServer push is not like long polling because the connection is shared. You could keep a stream open and push messages as needed. This already sort of is possible with fetch in the browser, but iirc there are some things the spec authors are working on for this use case.
- manigandham 9y agoYou can't do much with these assets. These are plain network requests as far as the browser is concerned so there's no API logic for your JS code to use. You're limited to just forcing the browser to download more things and it can also choose to ignore these push requests.
- Touche 9y agoYou can do whatever you want with them. For example, let's say you push /api/todos and your client-side code does a `fetch('/api/todos')`. This stream stays open, and the client code can receive each message as they come in, using ReadableStream. Now the server, having this connection open can later push more data into this /api/todos stream. Boom, you have websockets. Without needing a separate port and connection.
- manigandham 9y agoYou just described long-polling: holding the connection open until the response arrives or it times out. Even if you just send push-promises and push-responses while keeping the original response stalled (and the browser accepts the response), the client-side JS still has no idea what those requests were or what was loaded. SSE is the evolution of long-polling so that browsers already include the logic to keep a connection open with streaming updates so that you can avoid all this in the first place.
- Touche 9y agoThis differs from long polling in that http2 doesn't create a new connection. Also long-polling is request and then response, whereas http2 is (like websockets) full duplex.