Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to intercept and cache WebSocket messages in a Service Worker like all the examples do for normal HTTP requests?

I know you can create WebSocket connections from within a Service Worker itself; my question is more whether or not you can use a WebSocket from your app as normal and have the Service Worker intercept / cache WebSocket requests just like it can do for normal HTTP fetch requests?

Here's an example of intercepting and caching a normal HTTP request from a Service Worker.

self.addEventListener('fetch', function(event) {
  // If a match isn't found in the cache, the response
  // will look like a connection error
  event.respondWith(caches.match(event.request));
});

How would I setup the Service Worker if all of my requests were via WebSockets?

like image 363
fisch2 Avatar asked Jun 10 '16 06:06

fisch2


People also ask

Can WebSockets be intercepted?

You can use Burp Proxy to intercept and modify WebSocket messages, as follows: Open Burp's browser. Browse to the application function that uses WebSockets. You can determine that WebSockets are being used by using the application and looking for entries appearing in the WebSockets history tab within Burp Proxy.

Can a WebSocket have multiple connections?

A server can open WebSocket connections with multiple clients—even multiple connections with the same client. It can then message one, some, or all of these clients. Practically, this means multiple people can connect to our chat app, and we can message some of them at a time.

Would WebSockets be able to handle 1000000 concurrent connections?

With at least 30 GiB RAM you can handle 1 million concurrent sockets.

Are WebSockets Faster Than Rest API?

Fast Reaction TimeWebSockets allow for a higher amount of efficiency compared to REST because they do not require the HTTP request/response overhead for each message sent and received.


1 Answers

It's not possible for a service worker to intercept Web Socket traffic.

The service worker's fetch event is triggered only for controlled clients' HTTPS requests, and the message event is triggered only for postMessage() requests from clients.

While there is an HTTP handshake when initiating a Web Socket connection, that handshake does not trigger a fetch handler, and neither does the actual traffic sent once the connection has been established.

like image 192
Jeff Posnick Avatar answered Sep 25 '22 06:09

Jeff Posnick