Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPDY as replacement for Websockets?

Tags:

First off - I understand SPDY and Websockets aren't the same thing, and that you can run Websockets over SPDY like you can with HTTP, etc.

However - I am wondering if SPDY would be a viable replacement for websockets if I am trying to provide a REST (like) API that also supports server push (bi-directional calls over the same connection).

My current prototype uses websockets (node+socket.io), and works fine. However, my issue with websockets is I am having to dream up my own JSON protocol for routing requests both to and from the server. I'd much rather use REST-style URIs and Headers in requests, which fits better in a REST-based architecture. SPDY seems like it would support this better.

Also, because of the lack of headers, I'm concerned websockets won't fit well in our deployment network, and thinking SPDY would be a better fit again.

However, I've not seen many examples of bidirectional SPDY requests, apart from pushing files to the browser. I would like to push events and data to the browsers, such as:

Content-Type: application/json {    "id": "ca823f3e233233",    "name": "Greg Brady" } 

but it's not clear to me how the browser/Javascript might "listen" and react to these, as I would with the WebSocket and socket.io APIs.

like image 444
7zark7 Avatar asked Aug 24 '12 05:08

7zark7


People also ask

What will replace WebSockets?

SSE is an excellent alternative to WebSockets. They are limited to the browser's connection pool limit of ~6 concurrent HTTP connections per server, but they provide a standard way of pushing data from the server to the clients over HTTP, which load balancers and proxies understand out-of-the-box.

Can HTTP 2 replace WebSockets?

HTTP/2 is not a replacement for push technologies such as WebSocket or SSE. HTTP/2 Push server can only be processed by browsers, not by applications.

Is WebSockets obsolete?

Websockets are largely obsolete because nowadays, if you create a HTTP/2 fetch request, any existing keepalive connection to that server is used, so the overhead that pre-HTTP/2 XHR connections needed is lost and with it the advantage of Websockets.

Does HTTP 2 support WebSockets?

Feature: WebSockets over HTTP/2Supports WebSockets over HTTP/2 in Chromium as specified in RFC 8441 https://tools.ietf.org/html/rfc8441.


1 Answers

Let's start from the beginning: why would you want to run WebSockets over SPDY, as opposed to doing an HTTP upgrade? If you upgrade an HTTP connection to WS, then nothing else can use that TCP stream - the WS connection can be idle, but the connection is blocked nonetheless. With SPDY, you can mux multiple requests/responses, and a websocket connection (or even multiple) over the same underlying TCP stream. On a practical note, as of July 2012, WS over SPDY is still a work in progress, so you will have to wait to use SPDY for WebSockets - hopefully not too long though!

But let's assume the support is there... The reason why it's not clear how to listen for "SPDY Push" from JavaScript is because there is no way to do that! A pushed resource goes into your browsers cache - nothing more, nothing less. If you need to stream data to your javascript callbacks, then WebSockets, or Server-Sent Events (SSE) is the answer.

So, putting it all together:

  • HTTP adds a lot of overhead for individual small requests (headers, etc)
  • WebSockets gives you a low overhead channel, but requires you implement own routing
  • SPDY will significantly reduce the overhead and cost of small HTTP requests (win)
  • SSE is a good, simple alternative to pushing data to the client (which works today, over SPDY)

You could use SPDY+SSE to meet your goals, and all of that communication can run over the same TCP channel. SPDY requests to the server, SSE push from the server.

like image 118
igrigorik Avatar answered Nov 16 '22 23:11

igrigorik