stompjs icon indicating copy to clipboard operation
stompjs copied to clipboard

Null-pointer when calling subscribe()

Open sbaechler opened this issue 3 years ago • 5 comments

I was getting a TypeError: Cannot read properties of undefined (reading 'subscribe'):

sentry-error

We pause subscriptions before a batch action that can trigger mutliple websocket events and resume them once the batch is done. In that case that was after 2 seconds.

For this we call unsubscribe on the subscriptions and subscribe shortly after.

I guess for some reason _disposeStompHandler was called in between.

sbaechler avatar Jul 13 '22 15:07 sbaechler

Please enable debug and attach the full console output.

kum-deepak avatar Jul 14 '22 03:07 kum-deepak

I would need some time to reproduce this issue. Since it was reported by Sentry means that it happened in a production environment. I'll still have to figure out why the connection was disposed.

From what I could see from the source code, Stomp does not check if _stompHandler is defined in the subscribe method.

I would add an invariant there that produces a better error message.

sbaechler avatar Jul 14 '22 09:07 sbaechler

@sbaechler were you able to solve the problem? If yes, please share how did you did it as I'm also facing the same issue.

rumit0406 avatar Aug 12 '22 19:08 rumit0406

Are there any updates on this issue?

v1talii-dev avatar Sep 10 '22 09:09 v1talii-dev

A long discussion on seemingly a simple issue.

The underlying STOMP connection is not guaranteed to be always there - it may vanish at any time. The reconnection logic will trigger upon discovering the loss of connection, however, there would be spans without an underlying connection. In addition, the subscriptions do not survive disconnects and need to be reestablished on successful reconnects.

So, the user code must check and should be willing to handle these scenarios. This can make the user code quite clumsy.

To simplify these, a few years back I wrote a wrapper for this library https://github.com/stomp-js/rx-stomp. This version exposes an alternate semantics:

  • The subscriptions (https://stomp-js.github.io/api-docs/latest/classes/RxStomp.html#watch) are reestablished on each reconnect. One can call watch even when there is no underlying STOMP connection (this will take effect from the next reconnect).
  • The publish (https://stomp-js.github.io/api-docs/latest/classes/RxStomp.html#publish) will locally queue the message if there is no underlying connection.
  • There are other similar items (like watching connection state, RPC, enhanced callbacks, etc.).

In my opinion when using from a long-running application (an App or SPA) rx-stomp is a better fit.

Based on feedback, I may write an article in greater depth.

kum-deepak avatar Sep 25 '22 16:09 kum-deepak