Back

Help with Appwrite Realtime

  • 0
  • Realtime
  • Cloud
amaturcodrgui
21 Jan, 2025, 23:08

so i'm testing a chat app feature that i'm making and i'm trying to chat between two accounds on different devices connected to the local development server but i have to refresh to see a new message that i type on my other device

here is my realtime subscription snippet let unsubscribe; const setupRealtimeConnection = () => { return client.subscribe( [ databases.${import.meta.env.VITE_DATABASE_COMMUNITIES_ID}.collections.${ import.meta.env.VITE_COLLECTION_ID_SERVER_MESSAGES }.documents ], (response) => { const { events, payload } = response; console.log(response); scrollToBottom(); if (payload.communityId !== id) return;

TypeScript
            if (events.includes('databases.*.collections.*.documents.create')) {
                messages.update((msgs) => {
                    return [...msgs, payload].sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
                });
            }
        }
    );
};



onMount(async () => {
    await listCommunities();
    unsubscribe = setupRealtimeConnection();
    scrollToBottom(); // Ensure scrolling is handled properly
    getData(); // Fetch initial data
});

onDestroy(() => {
    if (unsubscribe) unsubscribe(); // Clean up the subscription on component destroy
});
TL;DR
Developers are working on a chat app feature using Appwrite Realtime and struggling to see new messages without refreshing the page when chatting between two accounts on different devices. To fix this issue, try moving the `scrollToBottom()` function inside the `.create` condition block in the `setupRealtimeConnection` function. This should automatically scroll to the bottom whenever a new message is received, eliminating the need for manual refreshes.
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