Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nathan815's avatar

Laravel Echo / Socket.io session token cookie issue

I'm working on a Vue SPA adding realtime features using Laravel Echo, Laravel Echo Server, and Redis. Everything works great if the user is logged in on the original page load. Since it is a SPA, logging in is done through ajax. On page load it automatically connects to a public channel (this works fine). Additionally, either on page load or when the user logs in successfully, it connects and subscribes to a user-specific private channel.

beginChatPrivateListener({ commit, state, rootState }) {
        if(!rootState.auth.isLoggedIn) {
            return;
        }
        const channel = `chat.${rootState.auth.user.id}`;
        echo.private(channel).listen('NewChatMessage', (e) => commit('receivedNewMessage', e.message));
        console.log(`chat: listening to private channel: ${channel}`);
        commit('setPrivateChannelName', channel);
    },

But it fails when the user logs in (Laravel Echo Server shows it as an authentication error, and the websocket receives a "subscription_error" message). I believe it is because when the user logs in the session token is refreshed, which updates fine in the browser but the websocket connection doesn't get the new cookie since it was opened with the previous session token cookie.

Is it possible to update the cookie on the websocket connection or make Laravel Echo/socket.io restart the connection?

0 likes
1 reply
Nathan815's avatar
Nathan815
OP
Best Answer
Level 1

Figured it out. The Echo instance has a "disconnect" method (not documented as far as I've seen), so what I'm doing is disconnecting and then reconnecting by making a new instance of Echo when the user logs in - and then dispatching a Vuex action for modules to reattach channel event listeners.

1 like

Please or to participate in this conversation.