Tutorial: WebSocket App¶
Let’s build a real-time app that exchanges messages between browser and server using Pyplet’s WebSocket protocol. We’ll use the chat example as a reference.
Client: handling input and messages¶
The client:
- Renders a text input and listens for changes
- Sends messages over the socket and appends new messages to the DOM
- Handles the server closing by checking for the
closing_messagesentinel
Server: broadcasting to all clients¶
The server loop:
- Assigns a username from a pool and sends an init handshake
- Broadcasts messages to all connected sockets
- Cleans up on disconnect
Run it¶
Start the server and open multiple browser tabs to the chat app’s URL. You should see messages broadcast in real-time.