Gamehoster Connect Functions

Gamehoster.connect is the low-level path for a game that wants to run its own loop instead of the abstraction. Gamehoster.start is built on top of it: it wires the render loop, input plumbing and per-object lifecycle for you. When you connect directly you get raw interpolated frames and drive rendering yourself.

The connection runs no game logic and does not predict. The server is authoritative; each display frame you read an interpolated view of the public state you can see, drawn a fixed buffer behind the server's leading edge so motion stays smooth under jitter. Your commands go up and take effect on the server's next tick.

Gamehoster.connect

Gamehoster.connect(game, join, opts?) returns a conn. It connects the game socket and folds the server's per-player binary delta updates into a local snapshot store. game is the game name, join is the hello or join payload object, and opts.server overrides the socket base URL.

const conn = Gamehoster.connect('asteroids', { name: 'Ada' })
Connecting to a game socket

The socket base can also be set with window.GAMEHOSTER_SERVER (wss://<domain>); the game name is appended to it. With neither set, the base is this page's own origin.

The conn object

What connect returns. Its fields report the connection, its callbacks tell you when the connection is ready or lost or has new state, and its methods send commands and read frames.

MemberWhat it does
conn.gameThe game name.
conn.selfYour own netId. Always 1.
conn.instanceThe instance id. null until welcome.
conn.connectedBoolean. false until welcome.
conn.tickRateThe server tick rate. 0 until welcome.
conn.onReady(cb)Welcome received, updates flowing. Fires immediately if already connected.
conn.onError(cb)Socket closed, schema mismatch or lost connection. cb gets a why string.
conn.onUpdate(cb)Run cb after each server update is folded in.
conn.send(type, params, predict?)Issue a command, applied on the server's next tick. Returns a seq number. The optional predict is a patch object or a function(view) that shows the intended result immediately.
conn.frame()Advance the render clock and return the interpolated view for this display frame, or null if no sim yet.
conn.now()The lead tick.
conn.lead()Lead ticks.
conn.input(name, value)Feed the local live input the game's predict body reads.
conn.latest(id)The newest authoritative, un-interpolated state of an object. An id of null returns the whole newest view.
conn.close()Close the socket.

The frame view conn.frame() hands you holds self, tick, instance, players[] and entities[]. You draw it directly.

const conn = Gamehoster.connect('pong', { name: 'Ada' })

conn.onReady(() => {
  requestAnimationFrame(function draw() {
    const view = conn.frame()      // interpolated, ready to draw
    if (view) render(view)
    requestAnimationFrame(draw)
  })
})

conn.onError((why) => showError(why))

// issue a command; it applies on the server's next tick
conn.send('move', { y })
A manual render loop over the raw connection