question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocketSubject fails to recreate socket when close isn't immediate

See original GitHub issue

Bug 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
markmcdowellcommented, Apr 5, 2019

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.

1reaction
ahnpnlcommented, Aug 22, 2019

Another approach to fixing this might be to set a property on the class (like _socketOpening) when a socket is opened via _connectSocket to true and check for that in socket.onclose and not call resetState if that value is true. Then once socket.onopen is called, you would set that value to false.

I ran into the same issue and I also used this fix to solve my problem.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found