stompjs
stompjs copied to clipboard
Null-pointer when calling subscribe()
I was getting a TypeError: Cannot read properties of undefined (reading 'subscribe'):

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.
Please enable debug and attach the full console output.
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 were you able to solve the problem? If yes, please share how did you did it as I'm also facing the same issue.
Are there any updates on this issue?
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
watcheven 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.