Live Cursors

A live multiplayer cursors demo, with the complete code behind it.

Move your pointer over the stage below. When this page can reach a Slash Socket server, everyone on the page shares the stage: open it in a second window and each pointer shows up in the other. Press / over the stage to chat next to your cursor: everyone sees the line as you type it, Enter sends it and Escape cancels. The chip in the top corner sets how you show up: pick a name, or connect GitHub to put your handle and avatar on your cursor. With no server to reach, the stage shows a static preview instead.

Live cursors
Preview
Move your cursor here
Pierre
StacyPress / to chat
live-cursors.tsx

The tabs above hold everything the demo needs: the same component in React or Angular, plus the styles for the cursors. Each pointer move is reported with cursor_move, and the server broadcasts it to everyone else as cursor_update, along with the sender's color and name. Positions are sent in pixels together with the stage size (vw, vh), so every client draws them at the same relative spot, whatever size its stage is. Sending x: -1 means "my cursor left the stage", and users_update tells you when someone disconnects so you can remove their cursor.

  • Write positions straight to each cursor's element, not into React state. Cursors update up to 60 times a second per person, and re-rendering on every update is what makes them lag.
  • Move cursors with transform only and never put a CSS transition on the position. Easing a cursor's movement only adds delay.
  • Send at most one cursor_move per animation frame, and send it with volatile so a busy connection drops old positions instead of queueing them.

To try the demo against your own server, start it in Slash Socket and open slash.tools/socket/docs/live-cursors?socket=http://localhost:3001. Browsers may ask for permission before a public page can reach localhost. For a full Next.js setup with a shared connection and environment config, see the Next.js Example.