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?
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.
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.
With at least 30 GiB RAM you can handle 1 million concurrent sockets.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With