So the first one is the real time that we want to listen to. So in this case, Postgres changes, but it could be presence or broadcast, which we'll cover next. And then the next parameter is what we want our event handler to listen to. So in this situation, we want the public schema, and this is a required field. Most of the time it will be public, I think, for most people. The event is not optional, but you could have it as a wild card. So I'm only interested in inserts, I'm not interested in updates. So I specified inserts, but you could have a wild card. And the table, I'm only interested in game results. But that could be omitted if you wanted everything out of your database. And there are additional filters that you can add as well. And the third parameter is my callback, I just want to increment each time there's an insert, that's it. And then we subscribe, so it registers our client with the server. Really simple, right? Who thinks that's pretty simple? Few more, few more. Okay, good. Cool. Does anyone see anything wrong with this, though? And it's not, there's a missing semicolon or comma. This does work and run, but there is a scenario when this wouldn't work. Hey, shout out. Sorry, a lot of traffic. Yeah, that's a challenge with the real time with the database, that's correct, especially with security. Anything else?
So the problem is, if I lose internet, if I'm on a train and I lose a connection, then if I go back one, you will see that I have got 41 games played. So say I'm on the train, I lose internet, and four more games are completed, that won't increment, it's fire and forget. So when I get internet again, and a fifth game comes in, it will go to 42, but that's actually incorrect, it should be 46. So you could refresh the page to fix it, but that's not really a good solution. Because a WebSocket connection stays open indefinitely unless it's closed, or there's an interruption to the internet connection, as I mentioned. And it is, yeah, fire and forget. And I think that that helps with the performance and scalability. But then you do have challenges like this. And we can solve it quite easily, really. In the subscribe, so when it reconnects, we just go and collect the latest data again. So it only happens when the page loads, or if we reconnect. And the great thing with this is, you don't have to write much more code, because that function already existed in my code when the page first loads. So it's really, really straightforward, because we don't want to refresh the page. And then it will just continue to get future events.
Let's talk about presence. This is my favorite real-time feature from SuperBase, because this used to be really, really hard to do. So the games in progress that you saw on the leaderboard, that was done by the presence features. How many people are actually playing the game right now? You've seen this in Google Docs. At the top-right, you see how many people have a view in this document with their profile picture. And you hover it, and you get their name. Here, I'm just sending player name, and I'm just counting it. But you can customize it however you want. So presence allows us to track state between users. And it has two parts. So before, the previous example, I had one part. This has two parts. So this is on the player, so the people playing the game. So we have the channel. And this can be anything.
Comments