Servers
async def start(): | Runs once when each server boots, and may run forever, e.g. to listen on Redis |
server_id() | The id of the server the code is running on, e.g. "chat-1". From framework |
ws.headers["X-User-Id"] | The signed-in user's id |
async for message in ws: | Each message the phone sends, as a dict. Ends when the phone disconnects |
await ws.send(message) | Sends a dict to the phone. Raises ConnectionClosed if it has disconnected |
Redis pub/sub
await redis.publish(channel, text) | Sends text to every current subscriber of channel. Returns how many there were |
pubsub = redis.pubsub() | A connection for subscribing, separate from redis |
await pubsub.subscribe(channel, ...) | Starts receiving a channel's messages |
await pubsub.unsubscribe(channel, ...) | Stops receiving them |
async for item in pubsub.listen(): | Waits for each item, forever |
await pubsub.aclose() | Closes the subscribing connection, once you're done with it |
Messages are strings, so send a dict as json.dumps(message) and read it back with json.loads(item["data"]).
item["type"] | Item |
|---|---|
"message" | A message on a channel you subscribed to. item["channel"], item["data"] |
"subscribe" | Confirms a subscription. Carries no message |
Redis keys
await redis.hset(key, field, value) | Sets a field of a hash |
await redis.hget(key, field) | A field's value, or None |
await redis.hdel(key, field, ...) | Deletes fields |
Each run starts with fresh servers and an empty Redis.
Python · 45 linesStarter code · Ctrl/⌘ + Enter to run
SystemBroke
No runs yet.
Run the simulation to send this kata's traffic to your code. Hidden checks grade what it does.