stomp-js / stompjs

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

Out-going heartbeat not working in React Native #640

Closed seungholee-dev closed 1 week ago

seungholee-dev commented 4 weeks ago

Hi in below environment, seems like out-going heartbeat(PING) from the client is not working after establishing a connect with the backend server. I have enabled the debugging as per the documents. But, seems like it's not showing Heartbeat messages as it's not being sent to the backend server. Need some support here. FYI, the polyfills have been enabled after reading the FAQs

Environment

Code

ws.current = new Client({
            brokerURL: WS_URL,
            debug: (str) => {
                console.log(str)
            },
            reconnectDelay: 2000,
            heartbeatOutgoing: 20000,
            heartbeatIncoming: 0,
            connectHeaders: {
                'accept-version': Versions.V1_1,
                authorization: `Bearer ${token}`,
            },
            onConnect: async () => {
                const { allChatrooms } = await updateChatroomsAndMessages();
                subscribeToChatrooms(allChatrooms, token);
                setIsConnected(true);
            },
            onStompError: (frame) =>
                console.error(
                    "Broker reported error:",
                    frame.headers["message"],
                ),
            onWebSocketError: (error) =>
                console.error("WebSocket error:", error),
            onWebSocketClose: () => {
                console.log("WebSocket connection closed");
                setIsConnected(false);
            },
        });

        ws.current.activate();
kum-deepak commented 4 weeks ago

The Heartbeats depend on the parameters negotiated during the CONNECT phase. Please attach the entire console output so that I can assist you.

mikadev commented 1 week ago

Hello exactly the same @kum-deepak (not related to react i'm just using the js client)

image

    import { Client } from '@stomp/stompjs'

    let message

    let data = []

    var url = 'ws://localhost:8082/stomp'

    const client = new Client({
        brokerURL: url,

        heartbeatOutgoing: 2000,
        heartbeatIncoming: 2000,

        onConnect: () => {
            client.subscribe('/foo', frame => {
                message = null
                data.unshift(JSON.parse(frame.body))
                data = data
            })

            client.subscribe('/foo2', frame => {
                console.log(frame)
            })
        }
    })

    client.heartbeatOutgoing = 2000 // client will send heartbeats every 20000ms
    client.heartbeatIncoming = 2000

    client.activate()

    // setInterval(() => client.publish({}))

    function send() {
        client.publish({
            destination: '/foo',
            body: JSON.stringify({ data: message })
        })

        client.publish({
            destination: '/foo2',
            body: JSON.stringify({ data: message })
        })
    }

Console debug log:

[vite] connected.
+page.svelte?t=1732175300660:290 Opening Web Socket...
+page.svelte?t=1732175300660:290 Web Socket Opened...
+page.svelte?t=1732175300660:290 >>> CONNECT
accept-version:1.2,1.1,1.0
heart-beat:2000,2000

+page.svelte?t=1732175300660:290 Received data
+page.svelte?t=1732175300660:290 <<< CONNECTED
server:vertx-stomp/4.5.11
content-length:79

+page.svelte?t=1732175300660:290 connected to server vertx-stomp/4.5.11
+page.svelte?t=1732175300660:290 >>> SUBSCRIBE
id:sub-0
destination:/foo

+page.svelte?t=1732175300660:290 >>> SUBSCRIBE
id:sub-1
destination:/foo2

+page.svelte?t=1732175300660:290 Received data
+page.svelte?t=1732175300660:290 <<< PONG
+page.svelte?t=1732175300660:290 Received data
+page.svelte?t=1732175300660:290 <<< PONG
+page.svelte?t=1732175300660:290 Received data
+page.svelte?t=1732175300660:290 <<< PONG
+page.svelte?t=1732175300660:290 Connection closed to ws://localhost:8082/stomp

Here the binary frame send by the server

CONNECTED
server:vertx-stomp/4.5.11

heart-beat:4000,5000
session:0d448c28-ba29-4a5b-80fb-ccaa3c9d3a83
version:1.2

Ok i found the responsible code: It will stop at the first considered headers maybe there is too much \n from the server ?

image

mikadev commented 1 week ago

So in my case it was related to mt server so it was not a bug

kum-deepak commented 1 week ago

Thanks for the detailed report. I am closing this issue.