Back

NextJs Account Realtime connection with no trace

  • 1
  • Auth
  • Web
  • Realtime
  • Cloud
Tomko
4 Mar, 2025, 22:23

I'm currently trying out the realtime events and first i wanted to try out how the account related subscriptions work but even though the websocket is on (if im making changes i get errors about the socket is disconnected and reconnect try will happen) i dont get any response. The basic idea is that if I change my user name or email it will change it on the user context.

TypeScript
    const [user, setUser] = useState<User| null>(null);
    const client = Connection();
    const account = new Account(client); 

    useEffect(() => {
        console.log('useEffect');
        //subscribe to user changes
        const _subscribe = client.subscribe('account', response => {
            // Callback will be executed on all account events.
            console.log(response);
        });
        return () => {
            _subscribe();
        }
    }, []);

    useEffect(() => {
        async function getUser() {
            const user = await account.get();
            if (user) {
                const {name, email, prefs, labels} = user;
                setUser({id: user['$id'], name, email, prefs, labels});
                document.cookie = `a=${user['$id']}; path=/; max-age=31536000;`;
                toast.success('Sikeres bejelentkezés!');
            }
            return
        }
        getUser();    
    }, []);

Connection() is just a bundler for the new Client() but other than that everything is like in the docs. You need to put the subscription to a useEffect bcs of the SSR, and i cant have the user object as a useEffect dependency cuz then i will have an websocket error: WebSocket is closed before the connection is established Also tried to if a user is present before subscription but still nothing happend if there was an event on account

I could not find any relatable and detailed solutions so im open for any kind of help, thanks.

TL;DR
Developers are encountering issues with realtime account connections in NextJs, specifically related to authenticating users through websockets. The code snippet provided includes a useEffect hook for subscribing to user changes and retrieving user data through an Account class. The problem seems to be related to SSR and handling websocket errors. Solution: Based on the provided code, there are a few potential solutions: 1. Ensure that the websocket connection is properly authenticated by checking if an auth message is being sent over the websocket in the network tab. 2. Check for any errors or logs related to websocket disconnections and attempts to reconnect. 3. Confirm that the subscription to account events
Kenny
4 Mar, 2025, 22:41

If you go into the network tab and click on ws, do you see it sending over an auth message?

Tomko
4 Mar, 2025, 22:53
Tomko
4 Mar, 2025, 22:53

only these even after an event

Kenny
4 Mar, 2025, 22:56

Click in the pending one and see the messages

Kenny
4 Mar, 2025, 22:56

is there an auth message

Tomko
4 Mar, 2025, 22:59
Kenny
4 Mar, 2025, 22:59

How are you authenticating in your app? SSR?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more