Live cursors
Everyone on this page, and on the home page, shares one presence channel. Move over the canvas and your position goes straight to the other browsers.
Move your pointer here, drag a finger, or focus the canvas and use the arrow keys.
How it works
The channel name starts with presence-, so joining needs a join token signed with the app's
private key. This site's backend signs one with a name and a colour for each visitor; that data is what shows up
in the member list.
Positions are client events named client-move (send out, client_event
in): the server relays them to the other members without calling your backend. They are sent as fractions
of the canvas so pointers line up on any screen size, and throttled to one every 50 ms. A connection may
send 10 client events a second unless its app has a higher limit; this site's own app has one for the demo,
so an app of your own should send every 100 ms, as below.
The bot keeps the berries, up to 100. Someone who arrives gets what is on the canvas from it in one client
event sent to them alone (to), and the page believes it only because the server names the sender
as a bot. Anyone in a channel can send any client event, so the bot and the page check every position
before they use it.
const channel = pikoman.listenPresence("presence-cursors", authenticator);
// Fires on join and whenever someone arrives or leaves.
channel.members((members) => {
for (const [id, { name, color }] of members) drawPointer(id, name, color);
});
// Client messages are named client-… and carry the sender's id first.
channel.on("client-move", (id, { x, y }) => movePointer(id, x, y));
// 10 client events a second per connection, unless your app has a higher limit.
canvas.addEventListener("pointermove", throttle((e) => {
channel.send("client-move", toFraction(e));
}, 100));