Fast-paced games
Shooters, racing and action games need to send player movement many times a second and see everyone else move smoothly. NetStack has a realtime channel built for exactly that. It sits next to Functions:
| Realtime channel | Functions | |
|---|---|---|
| Use it for | Movement, aim, animation: anything sent every frame | Shots, damage, pickups, scores: anything that changes the game's rules or state |
| How often | Up to 60 times a second per player | A few times a second |
| Delivery | Latest value wins. A lost update is replaced by the next one | Every call gets an answer |
| Runs your code | No, so it is very fast | Yes, on the server |
How it works
- Each player sends their own state with
sendState, for example position, rotation and current animation. - The server keeps the newest state from every player and sends everyone one combined snapshot at a fixed rate (the tick rate, 30 times a second on NetStack Cloud).
- Each client draws other players slightly in the past, smoothly blended between snapshots, so movement never stutters.
- When a player shoots, your game calls a Function. The Function can rewind the target to where they were when the shooter saw them, and decide if the shot hit.
Send and draw movement (JavaScript)
const client = new NetStackClient({ url: "wss://space-raiders.netstack.network", roomId: "match-1", playerId: myId });
await client.connect();
function everyFrame() {
client.sendState({ pos: [me.x, me.y, me.z], yaw: me.yaw, anim: me.anim });
for (const id of client.getStatePlayerIds()) {
if (id === myId) continue;
const s = client.getInterpolatedState(id);
if (s) drawPlayer(id, s.pos, s.yaw, s.anim);
}
requestAnimationFrame(everyFrame);
}
everyFrame();
getInterpolatedState blends numbers, arrays of numbers and nested objects. Text and other values switch over halfway between snapshots. Draw your own player from local input, not from snapshots, so your controls feel instant.
Send and draw movement (Godot)
func _physics_process(_delta: float) -> void:
NetStack.send_state({"pos": [position.x, position.y, position.z], "yaw": rotation.y})
for id in NetStack.get_player_ids():
if id == NetStack.get_local_player_id():
continue
var s := NetStack.get_interpolated_state(id)
if s.has("pos"):
others[id].position = Vector3(s.pos[0], s.pos[1], s.pos[2])
Use the UDP transport for fast-paced games when you can. It avoids the delays TCP adds when a packet is lost.
Check hits on the server
Never let a client decide that it hit someone. Send the shot to a Function along with the server time the shooter saw (client.serverTime() minus the interpolation delay, or simply client.serverTime() - 100). The Function rewinds the target and checks:
export default async function (ctx, args) {
const shotTime = Math.max(args.time, ctx.now - 300);
const target = ctx.room.stateAt(args.targetId, shotTime);
const shooter = ctx.room.state(ctx.player.id);
if (!target || !shooter) throw new NetStackError("NO_TARGET", "Target not found.");
const dx = target.pos[0] - shooter.pos[0];
const dz = target.pos[2] - shooter.pos[2];
if (Math.hypot(dx, dz) > 50) throw new NetStackError("OUT_OF_RANGE", "Too far away.");
const victim = ctx.players.find((p) => p.id === args.targetId);
if (!victim) throw new NetStackError("NO_TARGET", "Target not found.");
const health = (victim.get("health") ?? 100) - 25;
victim.set("health", health);
ctx.room.broadcast("hit", { by: ctx.player.id, target: args.targetId, health });
return { hit: true };
}
ctx.nowis the server time in milliseconds.ctx.room.state(playerId)is the player's newest state.ctx.room.stateAt(playerId, time)is their state at that moment in the last second, blended between updates.- Clamp how far back a shot may rewind (300 ms above) so players with very high ping cannot shoot into the past.
Keep cheaters in check
States come from players, so treat them like any other client input:
- Check movement in a Function when it matters. For example, before granting a pickup, check that
ctx.room.state(ctx.player.id)is actually near it. - Keep anything valuable (health, ammo, score, inventory) in player properties written by Functions, never in states.
- State size and rate depend on the plan: 512 bytes of JSON and 70 states a second on Free, 1 KB and 120 a second on Small Game and Indie, 2 KB and 240 a second on Studio, and 4 KB and 240 a second on Build Your Own. Extra states are dropped. The
realtimefield of thejoinedmessage tells the client its limits.
Timing and connection quality
client.rttis the round-trip time to the server in milliseconds, updated every 2 seconds.client.serverTime()is the server clock, corrected for network delay. Use it for anything you compare withctx.now.- The client reconnects by itself if the connection drops, and fires
reconnectingandreconnectedevents so you can show a message.
Limits worth knowing
NetStack runs your rules in Functions and relays movement, but it does not simulate physics on the server. Players move themselves and the server checks what matters. This is how most fast-paced indie multiplayer games work. If your game needs a full server-side physics simulation or deterministic rollback netcode, NetStack is not the right fit yet.