Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Old websockets do not seem to be terminated #170

Open
chongma opened this issue Nov 20, 2021 · 5 comments
Open

Old websockets do not seem to be terminated #170

chongma opened this issue Nov 20, 2021 · 5 comments

Comments

@chongma
Copy link

chongma commented Nov 20, 2021

I am having a strange issue with websockets. I have my websocket server behind an apache httpd 2.4.51 proxy and the sockets keep disconnecting after 30 seconds or so. I am trying to investigate the issue but in the meantime I have installed reconnecting-websockets and it seems to work.

But now I have discovered that the chrome tab eventually runs out of memory and looking into the network tab on chrome reveals that lots of duplicate websockets are being created and they have status "101" whereas sometimes i get one that says status "finished"

Screenshot from 2021-11-20 19-34-34

I am using VueX to try to store a single instance of each reconnecting websocket, essentially like this

function urlProviderAutorespond() {
    return `${process.env.VUE_APP_CT_WS_V1}/websockets/admin/autoreply?access_token=${Vue.prototype.$keycloak.token}`;
}

initialiseWsAutorespond({ state, commit }) {
        if (!state.wsAutorespond) {
            const ws = new ReconnectingWebSocket(urlProviderAutorespond);
            ws.onmessage = (event) => {
                commit('setAutorespond', JSON.parse(event.data));
            };
            ws.onopen = async () => {
                if (state.firstAutorespond) {
                    // fetch some data
                    commit('setFirstAutorespond', false);
                }
                console.log("Autorespond websocket opening...");
            };
            commit('setWsAutorespond', ws);
        }
    }

The issue may be related to the way that the server is terminating the connection but reconnecting-websockets shouldn't create a new one while the old one is still open?

@dce99
Copy link

dce99 commented Jul 9, 2022

Can the authors comment on this??
Problem -> On reconnecting, ws.close is called and then trying to connect or making a new web socket connection.

Isn't this ambiguous as a connection might still be open, and we might be opening a new one. Then server may send the close frame and onclose will be fired on client side, closing the new working connection !!

@chongma
Copy link
Author

chongma commented Jul 11, 2022

My problem turned out to be ping pong

@DamiToma
Copy link

@chongma Could you please elaborate on this? Did the old connection keep pinging the server even after closing?

@chongma
Copy link
Author

chongma commented Jul 13, 2023

@DamiToma The problem was on the server side. It wasn't correctly handling the ping pong. Eventually I moved to graphql subscriptions. So I didn't need to manually configure websockets any more.

@chongma
Copy link
Author

chongma commented Jul 13, 2023

@DamiToma The server code is something like this where it keeps a heartbeat unless the socket is closed

function heartbeat() {
    this.isAlive = true;
}

function initialiseWebsocket(name) {
    const wss = new WebSocketServer({ noServer: true });
    wss.on('connection', function (ws) {
        ws.isAlive = true;
        ws.on('pong', heartbeat);
    });
    const interval = setInterval(function ping() {
        wss.clients.forEach(function each(ws) {
            if (ws.isAlive === false) return ws.terminate();
            ws.isAlive = false;
            ws.ping();
        });
    }, 30000);
    wss.on('close', function close() {
        clearInterval(interval);
    });
    console.log(`Creating websocket ${name}`)
    return wss
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants