Skip to main content

Replication

client.udp is the Replication API over native UDP.

await game.udp.connect(minted)
game.udp.subscribe({"actor_update": lambda n: print(n.uuid, n.chunk, n.payload)})
await game.udp.send_actor_update((0, 0, 0), my_uuid, pose_bytes)
echo = await game.udp.send_actor_update_and_wait((0, 0, 0), my_uuid, pose_bytes)

Connecting mints nothing new. It calls serverWithLeastClients with your app token, waits for the session to be ready, then signs every datagram with that token. The handler names subscribe accepts are actor_update, voxel_update, audio, video, actor_left, text, client_event, server_event, generic_spatial, single_actor_message, channel_message, generic_error, status and any. From 0.41.0 there are two more, not_present and not_present_ended, for a heartbeat the server refuses.

Sends on client.udp cover actor updates, voxel updates, audio and video packets (send_video_frame fragments a whole frame), text, client events, single-actor messages, channel publishes and, since 0.15.0, generic spatial messages (send_generic_spatial, opcode 140). Idle heartbeats are on client.udp.connection. Each send returns its sequence number, and each *_and_wait variant resolves with the server's echo or raises on timeout. Sends are bundled into MESSAGE_BUNDLE datagrams; flush_sends() sends the pending bundle now.

From replication server v0.48.0 your own audio and video packets do not come back to you. To check capture by playing back what came round, call client.udp.set_own_media_echo(True) for the check and False after it (0.37.0; own_media_echo=True on connect starts with it on).

By default a player receives the audio and video of every actor in range. From replication server v0.49.0, client.udp.set_speakers(voice=[...], video=[...]) names whose it receives (0.38.0): a list receives only those actors (none when empty), None everyone again, and a kind you leave out stays as it is. Streams a list leaves out are not sent or billed. A list names at most the app's gameClientBootstrap.maxVoiceSpeakers / maxVideoSpeakers (32 at most); the connection sends the lists again with each capabilities advertisement.

An organization's own game server uses crowdypy.fleet.AsyncFleetSession (or the polled FleetSession) in place of client.udp: one session that carries all of the server's players (0.40.0). See Run your own game servers with a fleet session.

A connection takes only what the realtime server signed for its app​

From CrowdyPy 0.41.0 a player's connection drops two kinds of notification before any handler runs, and connection.stats() counts each:

A notificationCounted in
that nothing signedunsigned_notifications
that names another app than the connection's, however it was signedwrong_app

The realtime servers sign every spatial notification they send a session, and send a session only its own app's, so both counts stay 0. If one moves, tell Crowded Kingdoms. A connection made with verify_notifications=False still takes an unsigned notification.

Three more things changed in 0.41.0 with no change to your code:

  • A TOKEN_EXPIRED refusal reassigns only when it arrived inside a signed bundle. For one that arrived alone, the connection asks its session provider for a fresh token, at most once every 30 seconds, and does what the answer says. stats() counts those in unverified_token_expired. Through 0.40.0 the connection reassigned on either.
  • pump() reads 1,024 datagrams at most (RECEIVE_PASS_DATAGRAMS). In manual-pump mode, call it until it returns fewer.
  • disconnect() returns while datagrams are still arriving. The network thread stops reading at the 16 datagrams it has in hand.

A refused heartbeat is reported once​

From replication server v0.51.0, a heartbeat (send_heartbeat) is accepted only where the player's actor update would be (the rule). Where the player has no access, the server answers every heartbeat UNAUTHORIZED, and the actor is not present there. An older server never refuses a heartbeat, so with one no report is made.

CrowdyPy 0.41.0 reports the refusal once for each actor and place:

game.udp.subscribe(
{
"not_present": lambda report: hud.show_no_access(report.chunk),
# `end` is accepted, moved, or stopped.
"not_present_ended": lambda report, end: hud.clear(report.chunk),
}
)
report = game.udp.connection.not_present(my_uuid) # the report that stands, or None
  • report is an ActorNotPresent: uuid, chunk, sequence, and verified. end is a NotPresentEnd: accepted, moved, or stopped.
  • generic_error still receives every refusal. notification.keepalive is true for one that answers a heartbeat. stats() counts them in keepalives_refused, and the reports in not_present_reports.
  • Keep sending heartbeats at the same rate. Once access is granted, the next one is accepted within about 6 seconds, with no new sign-in.
  • Do not treat the player as present in that place while the report stands.
  • A world session reports the same through session.on("not_present", ...) and session.not_present().

A refused connection says why​

From CrowdyPy 0.41.0 a connection keeps the Application API's refusal of a server assignment as an ApiFailure: error, code, retry_after_ms, message, failed(), and platform_busy(). From 0.42.1 the SDK does not ask again after a refusal: through 0.41.0 a client with a discovery_url re-discovered and asked once more at once, a busy answer included.

from crowdypy.errors import CrowdyReplicationError

try:
await game.udp.connect(minted)
except CrowdyReplicationError as error:
if error.failure and error.failure.platform_busy():
await asyncio.sleep(error.failure.retry_after_ms / 1000) # then connect again
  • connection.last_assignment_failure() returns the same ApiFailure after the first connect, a later one, or a reassignment that failed.
  • retry_after_ms is 30,000 at most (MAX_RETRY_AFTER_WAIT_MS), whatever the server sent. The platform's own hints run from 200 ms to 10 seconds.
  • retry_after_ms_of(err) and platform_busy_of(err) still return what the server sent: pass it through retry_wait_ms before you wait on it.
  • A hub connection keeps its refusal the same way: see connect from a game.

Channel messages limited by distance​

send_ranged_channel_message (0.6.0) publishes to a channel but delivers only to the members with a live actor within max_distance chunks of an origin chunk, by straight-line distance with the boundary included. Members receive it through the same channel_message handler:

await game.udp.send_ranged_channel_message(
channel_id, my_uuid, b"over here", chunk=(10, 0, -4), max_distance=5
)

A member 3 chunks east and 4 north of the origin (exactly 5) receives it; one 4 east and 4 north (about 5.66) does not. The origin's app is the connection's app, max_distance is 0 to crowdypy.wire.CHANNEL_RANGED_MAX_DISTANCE (2147483647), it takes the same right as send_channel_message, and the sender receives no echo. See the channels guide.

Voice on a channel​

send_channel_audio (0.8.0, replication v0.37.0) sends audio to a channel: every other member receives it on the channel_audio handler (or session.on("channel_audio", ...)), wherever they are. The sender needs the channel's send_voice right (members_can_speak=True on channels.create gives it to every member) and use_voice_chat; a refusal is UNAUTHORIZED for the returned sequence. The payload is at most 1,024 bytes, and the crowdypy.media voice helpers below are an optional format for it. See voice on a channel.

packetizer = VoicePacketizer(VoiceCodec.OPUS, 20)
await game.udp.send_channel_audio(channel_id, my_uuid, packetizer.packetize(opus_frame))

Paused apps, voxel limits and echoes​

  • Error code 33, crowdypy.wire.ErrorCode.APP_PAUSED (replication v0.37.0), answers a send while the app is paused (no funds, a spend cap, a lapsed subscription). Keep the session and tell the player; AppTokenResponse.runtime_gate and is_app_paused(gate) say why at sign-in.
  • Voxel positions and types are your app's signed 16-bit values; a state over 1,024 bytes is refused before it is sent (crowdypy.wire.assert_voxel_edit). Your own accepted voxel edit always comes back once; other spatial messages come back only while your actor is in range.
  • Idle players on different servers see each other at the first full update after joining (replication v0.37.0), so the default keyframe and heartbeat cadence is enough. That update is when an idle actor first appears. Updates after that arrive at the send rate, including for a player on another server.

A game loop​

On a hot path, use the connection underneath. It sends a frame's entities in one call and reads whole batches of notifications:

conn = game.udp.connection
conn.send_actor_updates(chunks, uuids, poses, stride=88) # numpy: (n, 3) int64, n x 32 octets
async for batch in conn.batches():
actors = batch.types == crowdypy.wire.MessageType.ACTOR_UPDATE_NOTIFICATION
positions = batch.chunks[actors] # a view, not a copy

Each batch is a set of columns: types, app_ids, chunks (n × 3), uuids (n × 32), epoch_ms, sequences, the type-specific extras, and the payloads as offsets into one buffer. They are numpy views of native memory, and nothing is copied until you copy it.

crowdypy.replication.ReplicationConnection is the same connection for code without an event loop: wait(), then poll() from your own loop.

Video and audio​

crowdypy.media fragments video frames onto the wire and reassembles them (fragment_frame, VideoFrameAssembler), using the header format the other SDKs use. send_video_frame and send_audio_packet send them.

For voice, crowdypy.media (0.8.0) carries the optional convention the other SDKs share: a 10-byte header in front of each codec frame (encode_voice_packet, decode_voice_packet, which returns None for anything malformed), a VoicePacketizer, and a VoiceJitterBuffer keyed by sender that reorders, delays playout by 60 ms and reports missing frames as gaps (frame is None). The layout is on the CrowdyJS voice page. No codec ships: which one to use is your app's choice.