Back

[SOLVED] Realtime connection doesn't work

  • 0
  • Web
  • Realtime
manazo
10 Nov, 2023, 09:22

I've the below useEffect in my react (nextjs) app

TypeScript
  useEffect(() => {
    const channel = `databases.${DB_ID}.collections.${CHATS_COLLECTION_ID}.documents`;
    const unsubscribe = client.subscribe(channel, (response) => {
      console.log(response);
    });

    return () => unsubscribe();
  }, []);

I'm getting the client from a different file. it looks like below there

TypeScript
import { Client } from "appwrite";

const NEXT_PUBLIC_APPWRITE_ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT;
const NEXT_PUBLIC_APPWRITE_PROJECT_ID =
  process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID;

const client = new Client()
  .setEndpoint(NEXT_PUBLIC_APPWRITE_ENDPOINT)
  .setProject(NEXT_PUBLIC_APPWRITE_PROJECT_ID);

export { client };

But i'm getting no logs when a new document is created/updated/deleted in the corresponding collection. what am i doing wrong here?

Do the Collection permissions have anything to cause it? my collection only has the Create permission for all the users. and document level permissions enabled

TL;DR
Issue: Realtime connection is not working on localhost. No logs are being generated when a new document is created/updated/deleted in the collection. Suspected issue with collection permissions. Solution: 1. Make sure to set the `X-Fallback-Cookies` value in the client-side for client-side authentication. 2. Ensure that the authentication for the app is on the server-side and the realtime connection is only happening on the client-side. 3. Refer to the Appwrite documentation on realtime authentication to understand why unauthenticated users won't receive updates. 4. Realtime functionality may not work on localhost, so try running it in
ideclon
10 Nov, 2023, 11:14

Does your site have SSL?

manazo
10 Nov, 2023, 11:49

i'm running it in localhost

Appwrite: Cloud App: localhost

ideclon
10 Nov, 2023, 11:50

I don’t think Realtime will work in localhost

manazo
10 Nov, 2023, 11:51

really? why is that? and how folks do the development if it doesn't work on localhost

manazo
10 Nov, 2023, 12:13

in this video they're doing it in the localhost. and i literally have the exact same code for realtime

manazo
10 Nov, 2023, 12:26

i maybe getting the idea now. most of my app is SSR. my whole authentication is also on the server-side. i only doing the realtime connection on the client-side cuz it's obviously not possible to do it on the server-side. and according to this section, unauthenticated users won't receive any updates. so when I'm sending a request from the client-side, it's counted as an unauthenticated request to appwrite

manazo
10 Nov, 2023, 16:14

this was the main issue. solved by setting X-Fallback-Cookies value in the client-side for client-side authentication

manazo
10 Nov, 2023, 16:14

[SOLVED] Realtime connection doesn't work

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