Skip to main content

Event Handlers

Your WebSocket function must define these event handlers:

on_connect()

Called when a player connects to the WebSocket.
Returns: Dictionary that will be sent to the connecting player

on_message()

Called when a player sends a message to the server.
Returns: Dictionary that will be sent back to the message sender

on_disconnect()

Called when a player disconnects (closes WebSocket or loses connection).

on_tick()

Called repeatedly at a fixed rate (default 20 times per second) for continuous game updates.
To use on_tick, you must start the game loop by calling room.start_game_loop() in your on_connect handler.

Available Objects

session

Access player session data and information.

Properties

player_id
string
Unique identifier for this player
user
object | null
Authenticated user object (if JWT token provided)
room_id
string
Current room ID the player is in
connected_at
datetime
Timestamp when player connected
metadata
dict
Custom session metadata dictionary

Methods

set(key, value)
function
Store custom session data
get(key, default=None)
function
Retrieve custom session data

room

Manage rooms and broadcast messages to players.

Properties

id
string
Current room ID
state
dict
Shared room state accessible to all players. Initialize this in on_connect.
config
dict
Room configuration (tick_rate, custom flags, etc.)
metadata
dict
Custom room metadata for room listing

Methods

join(room_id, max_players=None)
function
Join a room (auto-created if doesn’t exist)
broadcast(message, exclude=None, include=None)
async function
Send message to all players in room
send_to(player_id, message)
async function
Send message to a specific player
get_players()
function
Get list of connected player IDs
get_player_count()
function
Get number of connected players
start_game_loop()
function
Start the game loop (enables on_tick calls)
stop_game_loop()
function
Stop the game loop
destroy()
function
Destroy the room and disconnect all players

request

Access incoming message data from the client.

Methods

get(key, default=None)
function
Get value from incoming message
json()
function
Get entire message as dictionary

db

Access your project’s database (same as HTTP functions).
See the Database Documentation for full database API reference.

Built-in Modules

The following Python modules are available in your functions:
time
module
Time-related functions
random
module
Random number generation
json
module
JSON encoding/decoding
uuid
module
UUID generation
datetime
module
Date and time handling

Rate Limiting

Each player is limited to 60 messages per second to prevent abuse. Messages exceeding this limit will receive an error response:

Room Listing

List all active rooms for your project:
Response: