Networking

Server

Authoritative server with command buffer, tick loop, and state sync.

The Server class is the authoritative multiplayer server. It manages client connections, processes commands, and broadcasts world state.

Setup

import { Server, BaseGame } from '@mavonengine/core'
import type { Channel } from '@geckos.io/server'

class MyServer extends Server {
  onConnection(channel: Channel): void {
    // A new client connected
    console.log('client connected:', channel.id)
  }

  onCommand(command: unknown, delta: number): void {
    // Process an incoming client command
  }

  getStateSyncDistance(): number {
    // Return the visibility radius (units) for state sync
    return 100
  }
}

const game = MyBaseGame.instance()
const server = new MyServer(game)
server.start()

Ports

PortPurpose
8081geckos.io WebRTC socket server
8050HTTP server (health check, meta endpoints)

Abstract methods

MethodDescription
onConnection(channel)Called when a new client connects
onCommand(command, delta)Called for each queued command on each tick
getStateSyncDistance()Returns the max distance for state sync visibility

Command buffer

Clients send commands (e.g. input frames) to the server. The server queues them and processes them in order on each tick via onCommand. This prevents cheating and ensures deterministic simulation.

HTTP endpoints

Register custom HTTP routes using onHttp:

server.onHttp((app) => {
  app.get('/status', (req, res) => {
    res.json({ players: Object.keys(world.entities).length })
  })
})

A built-in /health endpoint is registered automatically.

Bandwidth tracking

The server tracks outgoing bandwidth per tick. Access it via server.bandwidthTracker for monitoring.

ServerWorld

For the server-side world (with player/client tracking), use ServerWorld:

import { ServerWorld } from '@mavonengine/core'

ServerWorld extends BaseWorld and adds player management for connected clients.