Support the ongoing development of Laravel.io →
Authentication Requests

I'm working on a Vue SPA adding realtime features using Laravel Echo, Laravel Echo Server, and Redis. 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);
    },

Everything works great if the user is logged in when the page first loads. But it fails to connect 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?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Nathan815 nathan815 Joined 13 Aug 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.