Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell when a Stomp server disconnected from the Stomp.JS client

Tags:

sockjs

stomp

I am sending a stomp message over a Sock.JS client. When I disconnect the server I would like a warning message to show up on the client. To do this I have implemented a server side heartbeat

stompClient = Stomp.over(socket);
stompClient.heartbeat.outgoing = 20000;
stompClient.heartbeat.incoming = 20000;
stompClient.connect({}, function(frame) {
  ...
}

In the Chrome developer console I see the message

POST http://localhost:8080/hello/800/8n_btbxb/xhr_streaming net::ERR_CONNECTION_RESET sockjs-0.3.min.js:27
Whoops! Lost connection to undefined 

How can I capture this error message?

like image 922
Jackie Avatar asked Jul 01 '14 15:07

Jackie


People also ask

How do you use Stompjs?

STOMP JavaScript clients will communicate to a STOMP server using a ws:// URL. To create a STOMP client JavaScript object, you need to call Stomp. client(url) with the URL corresponding to the server's WebSocket endpoint: var url = "ws://localhost:15674/ws"; var client = Stomp.

What is STOMP over WebSocket?

STOMP is derived on top of WebSockets. STOMP just mentions a few specific ways on how the message frames are exchanged between the client and the server using WebSockets. Long Answer. WebSockets. It is a specification to allow asynchronous bidirectional communication between a client and a server.

How does STOMP work?

STOMP is the Simple (or Streaming) Text Oriented Messaging Protocol. It uses a set of commands like CONNECT, SEND, or SUBSCRIBE to manage the conversation. STOMP clients, written in any language, can talk with any message broker supporting the protocol.

What is RxStomp?

RxStomp. This library provides an RxJS oriented STOMP over WebSocket client for Web browser and node. js applications.


Video Answer


1 Answers

As pointed out by muttonUp stomp.js from https://github.com/jmesnil/stomp-websocket/ will overwrite the onclose handler. On the other hand it provides the option to pass an error-callback on connect:

stompClient.connect({}, function(frame) {
    ...
}, function(message) {
    // check message for disconnect
});

Since you will get several kinds of errors delivered to your callback, you have to check the message it it was indeed the "Whoops! [...]" which indicates a connection loss.

like image 159
Christoph Avatar answered Nov 22 '22 07:11

Christoph