What is a room? A room is the place that you enter in a Google Meeting, in Microsoft Teams, or in a file in Figma, for example, you have the idea of the room that everybody that is at this room will be synced together all the content, then you have the participant, the ID that usually is the email or some UID, and the name. Then you just create a new mouse pointers, passing the element, add it to the room, and it syncs and it does the whole magic.
But let's go even deeper than that. And let's see what is this mouse pointers? And how does this work between all of these devices? Well, the mouse pointers has three main functionalities. You see when someone moves their mouse, so you get the information about the position of other participants' pointers. You update your position to everyone, so you let everyone know about where your mouse is. And then you have to make it really fast and smooth, so it doesn't seem laggy, right? So let's see how we do each one of these.
The first one, we have this state here. This is a React thing, but you can be a simple JavaScript object with an X and a Y position. And we have this channel. I will talk more about it later, that I'm subscribing, that I'm listening. I'm also going to talk more about it later. One event called mouse move. When I hear something of this event, then I use this callback function on mouse move. When this callback function is triggered, I received a message. The message contains a data, and the data contains an X and a Y, and I set the mouse position to this position. And then you have to, of course, do the whole styles and the whole position top, salute, or something like that. But this is the mechanism behind it.
On the other side, we have to update our position to everyone. So we listen to the document on mouse move, same thing, same callback, that we received an event. We take the client X, the client Y to a position variable, and then we publish it. So we publish to the same event that we've seen before, the mouse moved, so everyone hears about it. Okay, now the heart. Make it really fast and smooth. Okay, so this is the same code that we've seen before, but with something else added, a native API from JavaScript that is requestAnimationFrame, that we make sure to update it in 30 FPS. But that's not the point here. Okay, let's move on.
What is this channel? Where does this come from? So the channel, we have this rule, we create a new real-time. Okay, so this real-time is a component that we sync data between devices and between users. On this real-time, we can connect to a channel.
Comments