when to unsubscribe from a table (supabase) - real-time

I subscribed to listening to inserts on a table in a front end application I have made using react, however in the documentation I've seen that the table is also unsubscribe from in many cases so was just wondering why you need to unsubscribe.
For context, the reason I subscribed was so that I can display the most recent value on the page.
Any answers to this would be greatly appreciated.

One typical case for unsubscribing would be when a user goes to another page, and you are no longer displaying the incoming realtime data to the user. In such case, you might want to unsubscribe.
In general, you subscribe from the realtime notifications whenever you no longer need the realtime data. In some cases, you might want to keep the realtime listeners connected at all times, because it's a data that is used in every single page of your application. In this case, you don't have to think about unsubscribing from the listener.

Related

check weather the user readed data or not in flutter using firestore

I was developing the chat app in flutter using firestore, the users successfully chat one-to-one. but I also want to send the notification to the user that someone sends him/her a message. For that I decided to use the cloud messaging service of firebase, but how can I check that the user read the message or not.
There is nothing built into Cloud Firestore to track which user has read a specific document already.
The two most common approaches that I know of are:
To track for each individual message and each individual user whether they have read it, so by keeping a list of the user IDs in each message document. While this potentially stores a lot of extra data, it is the most direct mapping of your use-case.
In most chat apps though, tracking of what messages each user has read is done as an "up to here". So if the user has read all messages up until a certain one or a certain time, the assumption is that they've also read all the messages before that one/moment.
For this model you can instead just keep a single document ID or timestamp for each user (i.e. readUpTo), instead of having to mark each message for each user that has read it. In your UI, you then determine whether to show each message as unread based on the message ID/timestamp compared to the timestamp of readUpTo.

Flutter Best Practices to Continuously Poll for New Data while on a Page which renders that Data?

I have an App written in Flutter, and I wish to have the App immediately render new Objects found in the Database, but the best way I can think of achieving this is firing a GET request to the Backend Database and call "setState()" every 5~10 seconds. This is so that when a user is in his "Order" table in-phone, and places a new "Order" from his/her laptop, I want that "Object" to show up in that users table in phone, without any page navigations or reloading the page.
The Table is implemented as a ListView.Builder()
If you manage also the backend I think one way could be to use websocket. When an order is inserted you call an API that emits an event to a websocket, your Flutter app that uses websocket can listen for this event and eventually rebuild the data.
In this way you don't need to call the GET api every N seconds but the server "calls you".
Here some flutter documentation about websockets: https://flutter.dev/docs/cookbook/networking/web-sockets

Firebase cloud functions chess game Swift

I am making a chess app which has a Firebase backend, i am using cloud firestore and cloud functions. basically i am using node 8 to avoid billing issues on cloud functions, i can call and trigger, but i can't wrap my head about how to make an action happen on another device instead of on my own.
After authenticating and logging in, the user gets into a lobby which is a tableViewController and there are shown only the users that are currently logged in.
What i want to achieve is by tapping on a certain row the user that got the tap on it gets an alert shown for him which states whether he accepts the challenge or he declines it. Based on that i proceed to the game or do something else.
The question is though how to make this alert trigger on the other user's device?
Also i've seen some posts that it can be done with snapshotListener on a document, but again i run into the problem of understanding how to trigger the alert on another device. If you've got some other good ideas please share!
Thank you for any feedback!
I think snapshot listeners are really the only way to go. You could use other Firebase services but those aren’t the right tools for the job. Consider creating a collection, maybe call it requests:
[requests]
<userId-userId> (recipientUserId-initiatorUserId)
initiator: string
recipient: string
date: date
Each user has a snapshot listener that listens to this collection where their own userId is equal to recipient. You can add a date field to sort the list by most recent, for example. When a user wants to challenge another user, all they need to do is create a document in this collection:
<userId-userId> (recipientUserId-initiatorUserId)
initiator: myUserId
recipient: theirUserId
date: now
And the recipient's client will instantly see this document.
You could include dressing data in this document, like the initiator's display name or a path to their avatar. But this data may be stale by the time it's rendered so the alternative is to fetch the dressing data from the database using the userId. You could also auto-gen the document ID but if you configure it (like this), it can make operations like deleting simpler. You could also configure the userIds to be alphanumerical so only one request can exist between two users; but if they requested each other at the same time, one would overwrite the other and you'd have to handle that potential edge case. There are lots of things to consider but this is the starting point.

XMPP: count of unread messages

I'm trying to implement chat for my webapp with following features:
When user logs in he should see a number of unread messages (which is both offline messages and "unseen", I will explain "unseen" in next step).
When user is anywhere in the app but on chat window he should be notified that he has a new message. Message should be marked "unseen" and must be added to the count of unread messages.
The first point is quite easily achieved using XEP-0013: Flexible Offline Message Retrieval. So I can retrieve offline messages and when I'm sure user has seen them - I remove them from unread list. But the problem is: how do I achieve same thing for "unseen" messages?
In short what I need is: any message should be marked as offline, unless user sees it and it's removed from the list by explicit request.
Can I achieve that with XMPP and how do I do that?
Thanks in advance.
What you are trying to do is to basically store a counter of unseen stuff in your account. I think you do not need flexible offline retrieval as when you connect the messages would simply become unseen. You thus only have to deal with one case: Unseen.
I will reply from the perspective of ejabberd, that I know better as one of the developer: I would use private storage to store your current state of unseen count and conversation.

scala lift comet

So basically I need a system like this:
We got users
Users have friends
Now the users if they come on the website they can post an "activity" just like twitter, they type in what they are doing, and all their friends get with a realtime update.
I have looked at lift for a week or three, and I digged into the chat server example, how ever as I said how can I make an comet actor for "activities" that where posted by friends?
I see two general approachs:
Do it the same way like the chat example: Just use one "Chat"server that holds all activities and every user is registred with. If a new activity is posted, every user would be informed and has to check if the activity was posted by one if its friends (via match/PartialFunction probably). If yes it display it, otherwise discard it.
Use one "Chat"server per user and register only the user's friends with this server. Note: I don't know if you need one comet listener per server for each of the user you are following in this case or if a comet listener can listen to several servers. If you need one listener per server, you will have to combine all activities you're listening to before displaying theese.
Approach 1 is closer to the ChatServer example but I would suggest to follow approach 2 since there's less communication in it so it should scale better. Also using different servers should improve scalability since you're able to do partitioning based on that. Of course you will have to some more management than using only one singleton "Chat"server.