3

I'm using this AutobahnJS code in node to receive data from a service. It works great, getting multiple events per second. When my internet temporarily disconnects Autobahn does not detect the lost connection and does not write "Websocket connection dropped" to console, it just hangs. Indefinitely.

Is there a timeout one can set, if no data arrives after 1 minute, reconnect? Or can I use a setTimeout function to ping a server and if no pong returns close the connection and try to reopen it?

I googled till my fingers were bleeding, but I didn't find a straightforward answer to this question. Thank you very much!

connection.onopen = function(session) {
    session.subscribe(arg, someEvent);
    }

connection.onclose = function() {
    console.log("Websocket connection dropped");
    }

connection.open();
peterh
  • 11,875
  • 18
  • 85
  • 108

1 Answers1

2

It is not possible to recognize an unclean disconnect without some data being sent. The WebSocket ping/pong mechanism at the protocol level is not exposed in the browser, and Autobahn|JS does not have any different handling when running in Node.js.

For the time being, you need to implement your own ping/pong mechanism at the application level.

gzost
  • 2,375
  • 1
  • 18
  • 25
  • When running on Node, AutobahnJS uses the "ws" library. This library might (or might not) expose WebSocket ping/pong. If it would, then we could use it in AutobahnJS. However, as gzost mentions, the W3C spec'ed browser WebSocket API simply does not expose it. Browser might (or might not) do WS ping/pong on their own (at least in the past, IE was doing so). – oberstet Apr 10 '16 at 21:59
  • 1
    I added an interval to check when I received the last data and if too much time passed issued a connection.close() and then in onclose() again a connection.open(). For the life of me I could not get it to work. Once the connection is closed it throws errors if you try to open it again?!? I tried Autobahn Python but the documentation and examples are so all over the place I couldn't figure out how to setProtocolOptions to AutoPing. I finally gave up and hacked it: after setInterval determines a timeout exit(), wrap it in a `while : do done` bash script. – heapoverflow Apr 11 '16 at 07:21
  • 1
    I'd be so very grateful for a working node or pythong example!! – heapoverflow Apr 11 '16 at 07:21