Skip to main content

Matchmaker Service

The CloudGaming matchmaker is a Node.js REST API that tracks available hosts and connects clients to game sessions automatically.

Architecture Overview

The matchmaker provides:
  • Host Heartbeats: Redis-backed host registration with TTL expiration
  • Match Finding: Weighted selection algorithm with region preferences
  • Dynamic ICE: Per-match TURN credential provisioning via Metered API
  • Health Checks: Railway-compatible liveness/readiness probes
  • Transaction Safety: Redis WATCH/MULTI for race-free slot allocation

REST API

Host Heartbeat Endpoint

Hosts send periodic heartbeats to register availability:

POST /api/host/heartbeat

Authentication: Bearer token (host secret)Request Body:
Response:

Implementation

Host State Tracking

Redis Keys:
  • host:{hostId}: Host metadata (JSON, 30s TTL)
  • idle_hosts: Set of host IDs with available slots
Status Values:
  • idle: Available for new connections
  • busy: All slots occupied
  • allocated: Reserved by matchmaker

Automatic Cleanup

Removes hosts whose heartbeat TTL expired.

Configuration

Environment Variables

Schema Validation

Ensures type safety and validation at runtime.

Client Integration

JavaScript Example

Key Source Files

Matchmaker.js

Main matchmaker implementation with host tracking and match finding logic.

config.js

Configuration loader for environment variables and defaults.
Best Practices:
  • Send heartbeats every 10-15 seconds (TTL is 30s)
  • Use region-based matching for lower latency
  • Monitor /api/hosts/ttl to detect failing hosts
  • Rotate HOST_SECRET periodically using HOST_SECRET_PREVIOUS