jmesnil / stomp-websocket

Stomp client for Web browsers and node.js apps
http://jmesnil.net/stomp-websocket/doc/
Apache License 2.0
1.43k stars 587 forks source link

Stomp client is not always fetching data even if the connection is established #136

Open alrms opened 7 years ago

alrms commented 7 years ago

Hello everyone, i'm using stomp client websocket for building a constant connection between my front-end layer and the integration layer. Sometimes in about 50/50 when i login the websocket does not fetch data to the console. Let me share you my code:

import SockJS from 'sockjs-client'; import { Stomp } from 'stompjs/lib/stomp'; import authTokenHandler from './authTokenHandler';

export const SockJSWrapper = { init: endpoint => SockJS(endpoint) };

/**

export default stompClient; When i login i dispatch an action which is used to establish a solid connection. The code is here: import { channel } from 'redux-saga'; import { put, take } from 'redux-saga/effects'; import { hashHistory } from 'react-router'; import { setAlertsNotifications, showNotification, setCurrentValue } from '../actions'; import stompClient from '../stompClient';

// Runs whenever a message is received form the web socket const onWebSocketMessage = (wsChannel) => { function wsMessageCallback(message) { const content = JSON.parse(message.body); const originatingService = message.headers.ocp_serviceName; switch (originatingService) { case 'insertNotifications': wsChannel.put(setAlertsNotifications('newNotifications', content.notifications)); wsChannel.put(setCurrentValue('unreadAlertsNum', content.unreadAlerts)); wsChannel.put(setCurrentValue('unreadNotificationsNum', content.unreadNotifications)); wsChannel.put(setCurrentValue('newAlertsNum', '')); wsChannel.put(setCurrentValue('newNotificationsNum', '')); // } else { // // error arrived // wsChannel.put( // setAccountInfo( // undefined, undefined, undefined, // content.ocp_errorCode, content.ocp_errorDescription) // ); break; default: wsChannel.put(setAlertsNotifications('newNotifications', content.notifications)); wsChannel.put(setCurrentValue('unreadAlertsNum', content.unreadAlerts)); wsChannel.put(setCurrentValue('unreadNotificationsNum', content.unreadNotifications)); wsChannel.put(setCurrentValue('newAlertsNum', '')); wsChannel.put(setCurrentValue('newNotificationsNum', '')); } } return wsMessageCallback; };

// Runs whenever a user is not authorized to view the system const onNoAuthCallback = (wsChannel) => { function wsNoAuthCallback() { hashHistory.push('/login'); wsChannel.put( showNotification('Session timeout', 'danger') // TODO clear store here ); } return wsNoAuthCallback; };

export default function setupWebsocket() { const wsChannel = channel(); const messageCallback = onWebSocketMessage(wsChannel); const noAuthCallback = onNoAuthCallback(wsChannel); stompClient.connect( noAuthCallback, [ { route: '/user/queue/command_response', callback: messageCallback } ]); / eslint-disable no-constant-condition / // Whenever an action is triggered from the callbacks // that belong to the wsChannel take it and dispatch it while (true) { / eslint-enable */ const action = yield take(wsChannel); yield put(action); } }

As a result, when a message arrived it should be pushed through the websocket. But sometimes, it is working properly, and sometimes even if the connection is established no data are pushed. Why is this happening? I 'm giving a lot of effort to solve the issue but nothing works

Last but not least, when the service status for the websocket is OK, the data are not fetched. When, the status is OK and the auth-token is changed the data are fetched

In the console, it also shows me this: Web Socket Opened... stomp.js:134 >>> CONNECT accept-version:1.1,1.0 heart-beat:10000,10000

<<< CONNECTED version:1.1 heart-beat:0,0 user-name:gpat

connected to server undefined stomp.js:134 >>> SUBSCRIBE id:sub-0 destination:/user/queue/command_response

fpozzobon commented 7 years ago

Hi, it can happen if you are connected to a queue ('/user/queue') and have more than one client listening to it.

hsolg commented 6 years ago

I have a similar problem. If I reload the page (triggering a new connect) less than one minute after I loaded it, the STOMP connection will connect successfully, but I don't receive any data from the queue I subscribed to. No error handler is called. If I wait more than one minute before reloading, I receive data normally after the reload. I am unsubscribing the topic and disconnecting the STOMP connection when the page is unloaded. I know that only one client is connecting to the server.

Did you find a solution to your problem?

fpozzobon commented 6 years ago

It sounds like you have an asynchronous problem, like for example you send unsubscribe after reconnecting... Best might be verifying on the server what it receives and in which order... If this is not already the case, using Promise or observables could help ;) (for example library https://github.com/fpozzobon/webstomp-obs :p )

denov commented 5 years ago

i've seen similar behavior against a spring (5) backend

ghost commented 4 years ago

I have had similar problems. Most of the time a server or even OS restart was the solution. The stomp client seems to get kinda buggy as soon as you ran more than one or two extra connections in one session.