Back

Realtime connection not working on NextJs

  • 1
  • Web
  • Realtime
  • Cloud
Tomko
5 Mar, 2025, 09:08

Hey, I'm currently working a nextJs projekt with appwrite and tried to connect to the 'account' channel but im not getting any response even on changes. After initial load i have opened another tab where i make changes to the user but nothing came in the page component. On the network tab there is the realtime connection pending but has no data and after a while its fail and try to esteblish a new connection.

TypeScript
'use client'
import React, { useEffect, useState } from 'react'
import { Client, Account } from 'appwrite';
import Connection from '@/lib/appwrite';

const Page = () => {
  const client = Connection();
  const account = new Account(client);
  const [user, setUser] = useState<any>(null);

    useEffect(() => {
        const fetchUser = async () => {
            try {
                const userData = await account.get();
                setUser(userData);
            } catch (error) {
                console.error('Error fetching user:', error);
            }
        };
        
        fetchUser();

        const unsubscribe = client.subscribe('account', (response) => {
            console.log('Account update received:', response);
            fetchUser(); 
        });

        return () => unsubscribe(); 
    }, []);
  return (
    <div>Page asd</div>
  )
}

export default Page 
TL;DR
Realtime connection to 'account' channel in Next.js project with Appwrite not working properly. Realtime updates not reflecting on the page component even after making changes. Realtime connection pending in network tab but fails to establish. Possible solution: Check if the `fetchUser` function is executing properly in the subscription callback.
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