Back

Realtime connection with authenticated session

  • 0
  • Self Hosted
  • Auth
  • Web
  • Realtime
maximilianls
4 Nov, 2024, 17:20

I'm trying to get a realtime connection set up in Next.js, in a client side component for "chats." I have tested my code with a collection that has "any" read permissions, and everything works well then, but when there is "users" read permissions on it doesnt. This leads me to believe that the session is not being "sent" when establishing the realtime connection. Does anyone know how to properly do this? (Session in console log is true and the correct session value)

TypeScript
const createClientSessionClient = async (session: any) => {
    console.log(`Setting session in createClientSessionClient: ${session ? 'true' : 'false'}`);
    const client = new Client()
        .setEndpoint(endpoint)
        .setProject(project)
                .setSession(session);

    return {
        get account() {
            return new Account(client);
        },
        get databases() {
            return new Databases(client);
        },
        get storage() {
            return new Storage(client);
        },
        get client() {
            return new Client().setEndpoint(endpoint).setProject(project).setSession(session);
        }
    };
};

export { createClientSessionClient, simpleClient };

Also the "get client()" used to only have: return client This didnt work either.

TL;DR
Developers are trying to set up a realtime connection in Next.js for chats. The issue seems to be with sending the authenticated session when establishing the connection. The provided code sets the session in `createClientSessionClient` but doesn't seem to work when users have read permissions. The initial code snippet has been updated to include `return new Client().setEndpoint(endpoint).setProject(project).setSession(session);` in the `get client()` section.
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