Back

[SOLVED] realtime *.delete not working as expected.

  • 0
  • Web
  • Realtime
Camo
30 May, 2023, 17:17

client.subscribe('databases.default.collections.rss_articles.documents.*.delete', callback)

Would not return anything, thus client.subscribe('databases.default.collections.rss_articles.documents, callback); would return a ton of information, but i want to filter for only deleted in this occasion

TL;DR
The user was experiencing issues with a code related to real-time deletion of posts. They updated the code to prevent duplicate keys and mentioned it may be helpful for others. They also had a question about assigning users to posts. Another user mentioned they can only subscribe to one real-time stream connection at a time. A code snippet was provided as a potential solution to process real-time events. The user also mentioned a potential concurrency problem and that the callback may be executing at the same time. They clarified that the channel they were using was incorrect and provided the correct supported channels documentation. They shared their current code for subscribing and processing changes, but mentioned that
Camo
30 May, 2023, 18:09

At the moment I do js subscribeArticles: async (callback) => { client.subscribe('databases.default.collections.rss_articles.documents', callback); }, then ```js AppwriteService.subscribeArticles(onArticleChange);

function onArticleChange(response) { const { events, payload } = response; if (events.includes("databases.default.collections.rss_articles.documents..delete")) { $posts = $posts.filter(post => post.$id !== payload.$id); } else if (events.includes("databases..collections.rss_articles.documents.*.create")) { $posts = [response.payload, ...$posts] } }``` What i have noticed is, if i add 20 articles really fast, it would skip some article add updates, any ideas?

Drake
30 May, 2023, 18:10

yes, because databases.default.collections.rss_articles.documents.*.delete is not a channel. the supported channels are: https://appwrite.io/docs/realtime#channels

Drake
30 May, 2023, 18:13

this is probably due to a concurrency problem. This callback is probably executing at the same time or so and they're overwriting each other

Camo
30 May, 2023, 18:22
TypeScript
const updateQueue = [];

    function processQueue() {
        while (updateQueue.length > 0) {
            const { events, payload } = updateQueue.shift();

            if (events.includes("databases.default.collections.rss_articles.documents.*.delete")) {
                $posts = $posts.filter(post => post.$id !== payload.$id);
            } else if (events.includes("databases.*.collections.rss_articles.documents.*.create")) {
                const existingPostIndex = $posts.findIndex(post => post.$id === payload.$id);
                if (existingPostIndex === -1) {
                    $posts = [payload, ...$posts];
                } else {
                    $posts[existingPostIndex] = payload;
                }
            }
        }
    }

    function onArticleChange(response) {
        updateQueue.push(response);
        processQueue();
    }
``` tried to come up with a queue solution, but got the same result. Got to think here..
Drake
30 May, 2023, 18:24

i think a lot of state management solutions make this easier...

Camo
30 May, 2023, 18:25

could you elaborate on that i don't understand? you mean like store? $posts is a store already, if you mean by that

Drake
30 May, 2023, 18:25

what framework are you using?

Camo
30 May, 2023, 18:25

sveltekit

Camo
30 May, 2023, 18:27

hmm, let me run some tests, it seems that it fixed the problem, i guess i didn't save the file before hand

Camo
30 May, 2023, 18:29

yeah it added 101 posts with no problem now

Camo
30 May, 2023, 18:31

@Steven i can subscribe to only one realtime stream connection right? I need to break the connection once i go to another page, if I want to subscribe to another one?

Drake
30 May, 2023, 19:16

up to you, though it would make sense not to subscribe to a channel you don't care about anymore

Camo
30 May, 2023, 19:50

Seems like a success for today

Camo
30 May, 2023, 19:50

got to figure out how to assign users to the posts from which are the feeds and should be golden

Camo
30 May, 2023, 19:53

i've updated the code, so it would not add duplicate keys, which could result in error, maybe someone will benefit from it

Camo
30 May, 2023, 19:59

[SOLVED] realtime *.delete not working as expected.

D5
30 May, 2023, 20:01

How did you have made this video? 🤩

Camo
30 May, 2023, 20:02

Screen Studio

D5
30 May, 2023, 20:02

Looks great, cogratulations

Camo
30 May, 2023, 20:02

thanks man, i thought deploying the costum functions would be much harder

Camo
30 May, 2023, 20:03

atleast my previous experience was

D5
30 May, 2023, 20:03

Me too first time, then I found It was really easy to work with 😁

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