Repository navigation
Example: Simple Client
BlackoutRoulette edited this page Aug 15, 2026
·
1 revision
This is the most basic example: connect to a server, log in, and print incoming messages.
- Creates a
Clientand overrideson_print_jsonto print chat messages - Connects to the server and sends a
Connectpacket to log in - Waits 5 seconds to receive messages, then disconnects
import asyncio
from archipelagopy import Client, packets, structs, enums
async def on_print_json(packet: packets.PrintJSON):
for msg in packet.data:
if msg.text is not None:
print(f">> {msg.text}")
async def main():
client = Client(port=12345)
# replace the default on_print_json callback with our own
client.on_print_json = on_print_json
# connect to the server
await client.start()
# send a connect packet to log in
await client.send(
packets.Connect(
version=structs.Version(major=6, minor=0, build=0),
tags=["AP"],
name="Link", # slot name
game="Ocarina of Time"
)
)
# wait for a while to receive packets
await asyncio.sleep(5)
# stop the client
await client.stop()
if __name__ == "__main__":
asyncio.run(main())>> Link (Team #1) playing Ocarina of Time has joined. Client(6.0.0), ['AP'].
>> Now that you are connected, you can use !help to list commands to run via the server. If your client supports it, you may have additional local commands you can list with /help.
-
asyncio.run(main())starts the asyncio event loop and runsmain(). All async code needs to run inside an event loop. -
Client(port=12345)creates a client configured to connect toarchipelago.ggon port 12345 using an encrypted connection. See Client Configuration for all options. -
client.on_print_json = on_print_jsonreplaces the default (empty)on_print_jsoncallback with our function. When the server sends aPrintJSONpacket (chat messages, status updates, etc.), our function will be called. Note that the replacement must beasync defto match the original — see Callbacks for details. -
await client.start()opens the WebSocket connection to the server. -
await client.send(packets.Connect(...))sends the authentication packet. Thenamemust match a slot name in the Archipelago game, andgamemust match the game for that slot. -
await asyncio.sleep(5)keeps the program running for 5 seconds so we can receive and print messages. -
await client.stop()closes the connection and cleans up.
For a cleaner way to manage the client lifecycle, see the Advanced Client example which uses async with (a context manager) instead of manual start()/stop().