WebSocketSubject fails to recreate socket when close isn't immediate
See original GitHub issueBug Report
Current Behavior If the close of a websocket takes a little bit of time subsequent calls to multiplex fail to send any subscription messages and close the socket immediately. See reproduction below.
Reproduction
import { interval } from "rxjs";
import { webSocket } from "rxjs/webSocket";
import { switchMap, take } from "rxjs/operators";
const socket = webSocket<any>({
url: "ws://localhost/ws"
});
interval(2_000)
.pipe(
take(2),
switchMap(id => {
return socket.multiplex(
() => `sub ${id}`,
() => `unsub ${id}`,
value => value === id
);
})
)
.subscribe({
next: _ => { },
error: err => console.error(err),
complete: () => console.log("Complete")
});
Expected behavior A new call to multiplex should subscribe and stay open.
Environment
- 6.4.0
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:7 (1 by maintainers)
Top Results From Across the Web
WebSocketSubject fails to recreate socket when close isn't ...
My use case here is I have 2 tabs in a UI, when I change tabs I unsubscribe to a stream, which starts...
Read more >Angular2 - How to close WebSocketSubject underlying socket
I was doing the unsubscribe and it wasn't closed. I end up using directly the websocket instead of WebSocketSubject, so I could call...
Read more >Troubleshooting connection issues | Socket.IO
IO client will always try to reconnect, unless specifically told otherwise. Let's review how you can troubleshoot a connection failure.
Read more >java websocket reconnect on disconnect
Websockets will not automatically try to reconnect. ... While closing connection, WebSocketSubject calls closeObserver where we are trying to restore it.
Read more >Deep Dive into RxJS WebSocket Subject at AngularNL 2020
Lamis Chebbi - Deep Dive into RxJS WebSocket SubjectAngularNL 2020 The Angular Conference of The Netherlands onb 24th July 2020.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

My use case here is I have 2 tabs in a UI, when I change tabs I unsubscribe to a stream, which starts to close the connection. The new tab makes a different subscribe but this fails as as the close isn’t complete.
My current work around is to delay new subscriptions by 250ms, but this is really dependant on how long the connection takes to close.
I ran into the same issue and I also used this fix to solve my problem.