stomp-js / stompjs

Javascript and Typescript Stomp client for Web browsers and node.js apps
Apache License 2.0
771 stars 81 forks source link

How I can catch reconnected #523

Closed Garam-Leee closed 9 months ago

Garam-Leee commented 1 year ago
 const client = new StompJs.Client({
      debug: (str) => {
        console.log(str);
      },
      heartbeatIncoming: 2000,
      heartbeatOutgoing: 2000,
      reconnectDelay: 8000,
      webSocketFactory: () => {
        return new SockJS(HOST_NAME + "/websocket");
      },
      onConnect: (frame) => {
        client.subscribe("/sub/noti/" + hospitalId, function (message) {
          parseMessage(JSON.parse(message.body));
        });
      },
    });

    client.activate();

After the server restarts and the socket connection is disconnected, how does it know when it is reconnected?

I knew that onConnect works for reconnect, but it doesn't seem to work that way.

Garam-Leee commented 1 year ago

Use 7.0.0 @stomp/stompjs and 1.6.1 sockjs-client

Garam-Leee commented 1 year ago

In case of normal connection

>>> CONNECT
accept-version:1.2,1.1,1.0
heart-beat:2000,2000

<<< CONNECTED
heart-beat:0,0
version:1.2
content-length:0

>>> SUBSCRIBE
id:sub-0
destination:/sub/noti/1

In case of server reboot, only console.log

>>> CONNECT
accept-version:1.2,1.1,1.0
heart-beat:2000,2000
kum-deepak commented 1 year ago

You are likely hit by SockJS and/or your broker limitations. Please see https://stomp-js.github.io/guide/stompjs/rx-stomp/using-stomp-with-sockjs.html

Please notice that the CONNECTED frame has heart-beat:0,0. That indicates that your setup has disabled heartbeats. Heartbeats are one of the mechanisms to detect a stale connection.

My initial suggestion will be to rewrite the code without using SockJS.