Search code examples
htmlwebsocketstompsockjsservice-worker

WebSocket (Sockjs and Stomp) with ServiceWorkers


I'm using Stomp and Sockjs to register a websocket with my Spring Boot self-contained server (3.1.1), which all works fine. However, I have implemented a ServiceWorker to handle offline services, which is all working, except for the socket. The call to ws://localhost/registerSocket yields this error on the frontend:

Firefox can't establish a connection to the server at ws://localhost/registerSocket/351/4vevdv79/websocket.

And this on the backend:

ERROR o.s.w.s.s.s.DefaultHandshakeHandler - Handshake failed due to invalid Upgrade header: null

In my ServiceWorker I've specifically excluded it from intercepting the ws request:

if (uri.indexOf("/registerSocket") === -1) {
   event.respondWith(getResponse(event));
}

Yet, inevitably the ws upgrade fails and it reverts to the old comet style polyfill.

Does anybody know of a way to get websockets working correctly with ServiceWorker?


Solution

  • I've just found out about a bug in Firefox 44 that prevented Web Socket connections from working when a Service Worker was used.

    See this bug report.

    The bug should be fixed now (since Firefox 44.0.1; it's even in the release notes).