Overview
In this guide, you’ll build a simple client-server application where the client sends messages to the server, and the server responds. This demonstrates the core concepts of repod:- Defining a
Channelsubclass for server-side client representation - Creating a
Serversubclass with custom connection handling - Building a
ConnectionListenersubclass for the client - Using action-based message dispatch with
Network_*callbacks - Running synchronous pump loops
Create the server
First, create a file named Next, subclass Set
server.py. You need to subclass two classes to make your own server.Each time a client connects, a new Channel instance is created, so you subclass Channel to make your server-side representation of a client:server.py
The
network_received() fallback is called if no specific handler exists. The method Network_myaction() is only called if your data has an "action" key with a value of "myaction".Server:server.py
channel_class to the Channel subclass you created above. The on_connect() method is called whenever a new client connects.To run the server, call launch():server.py
launch() handles the event loop internally and catches Ctrl+C for clean shutdown. Use host="0.0.0.0" to listen on all network interfaces.Create the client
Create a file named Network events are received by
client.py. To connect to your server, subclass ConnectionListener:client.py
Network_* callback methods. Replace * with the value of the "action" key you want to catch. The connected, disconnected, and error events are sent automatically by repod.Connect and pump:client.py
Run the server
Open a terminal and start the server:You should see output indicating the server is running (the exact output depends on your logging configuration). The server will block and wait for connections.
Complete code
Here are the complete files for reference:Understanding the flow
Let’s break down what happens when you run this example:Client connects
When
client.connect() is called, a background thread is started that establishes a TCP connection to the server. Once connected, the server:- Creates a new
ClientChannelinstance for this connection - Adds it to
server.channelslist - Calls
on_connect()callback - Sends
{"action": "connected"}to the client
Client receives connected event
The client’s pump loop receives the
{"action": "connected"} message and dispatches it to Network_connected(), which sends a message to the server.Server processes message
The server receives
{"action": "myaction", ...} and dispatches it to ClientChannel.Network_myaction(), which echoes a response back.Key concepts
Action-based dispatch
repod uses the"action" key in message dictionaries to route messages to the appropriate handler:
Synchronous pump loops
The client runs networking in a background thread, but you interact with it synchronously from your main game loop:pump() into the loop.
Sending data
Both client and server can send arbitrary data structures:Next steps
Core Concepts
Dive deeper into how repod works under the hood
Building a Server
Learn advanced server patterns and best practices
Building a Client
Integrate repod with game frameworks like pygame
Examples
Explore complete example projects with source code