Skip to content

What happens if sockJS gets disconnected? #38

Closed
@hunterloftis

Description

@hunterloftis

Will it automatically reconnect or should I code some custom reconnect into on('close')?

Activity

majek

majek commented on Jan 11, 2012

@majek
Member

You get the onclose event. Reconnection can't be done automatically by SockJS, so it's your responsibility to handle that accordingly.

hunterloftis

hunterloftis commented on Jan 11, 2012

@hunterloftis
Author

Then how should you handle the onclose event to reconnect? I don't see anything in the API docs like a 'connect' method... so would you have to create a new SockJS instance? In that case, how do you clean up all the event listeners you made for the old SockJS instance?

Thanks

majek

majek commented on Jan 11, 2012

@majek
Member

so would you have to create a new SockJS instance?

yes. it's similar to websockets API

In that case, how do you clean up all the event listeners you made for the old SockJS instance?

The naive assumption is that once you stop referring to the old sockjs instance it should be garbage collected. If that's not true - file a bug as this would be a memory leak!

joerussbowman

joerussbowman commented on Jan 13, 2012

@joerussbowman

I do just that with onclose, here's a sample of my javascript

$(function() {
    var chat, sockjs, offline_members;
    var sockjs_url = '/chat/sjs/';
    var reconnect = true;

    new_conn = function() {    
        sockjs = new SockJS(sockjs_url);

        sockjs.onmessage = function(e) {
            // chat(JSON.stringify(e.data));
            chat(e.data);
            // chat(e);
    };
        sockjs.onclose = function(e) {
            if (reconnect){ new_conn();}
        };
majek

majek commented on Jan 13, 2012

@majek
Member

This is perfectly fine. I would suggest some kind of delay and exponential backoff - you don't want to spin 100% cpu if the network is unavailable, right?

joerussbowman

joerussbowman commented on Jan 13, 2012

@joerussbowman

yea, it's on my todo already to set up exponential backoff, one of the first things I plan on doing after I finish my backend rewrite to use tornado.gen where possible.

knowitnothing

knowitnothing commented on Oct 24, 2013

@knowitnothing

I've commited a simple reconnector at https://github.com/knowitnothing/sockjs_reconnect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @majek@joerussbowman@hunterloftis@knowitnothing

        Issue actions

          What happens if sockJS gets disconnected? · Issue #38 · sockjs/sockjs-client