Connecting a Python Client to Vuer
Overview
Vuer provides a VuerClient class that allows you to connect to a running Vuer server from a separate Python process. This is useful for:
- Sending custom events to the server from external scripts
- Building distributed applications with multiple clients
- Testing and debugging Vuer event handlers
- Integrating with other Python applications
Basic Setup
Step 1: Start a Vuer Server with Event Handlers
First, start a Vuer server with handlers for client events:
Run the server:
Step 2: Connect with a Client
In a separate terminal, run a client that sends events to the server:
Run the client:
View the result in the browser at vuer.ai?ws=ws://localhost:8012.
Client API Reference
Creating a Client
Configuration can also be set via environment variables:
VUER_CLIENT_URI: WebSocket URI (defaultws://localhost:8012)WEBSOCKET_MAX_SIZE: Maximum message size in bytes (default 256MB)VUER_SSL_VERIFY: Whether to verify SSL certificates (defaulttrue)
Context Manager (Recommended)
Manual Connection
INIT Event (Automatic)
When a VuerClient connects, it automatically sends an INIT event with system information. This allows the server to identify the client type and environment.
The INIT event includes:
On the server, you can await this event to identify the client:
Sending Events
Define custom event classes by setting etype as a class attribute:
You can also set a default value:
Receiving Events
Complete Example: Remote Control
Here's a complete example where a client remotely controls an animated box using a custom event class:
server.py - Runs the Vuer server:
client.py - Sends animated position updates with a custom event class:
Run the server first, then run the client in a separate terminal to see the animated box.
Tips
- Define custom events by subclassing
ClientEventwith anetypeclass attribute - The server handles events via
@app.add_handler("EVENT_TYPE") - Use
client.send @ Event()for fire-and-forget (no await needed) - Use
await client.send(Event())when you need to wait for the send to complete - Use
client.connectedto check connection status - Reconnect by calling
connect()again if disconnected - Configure via environment variables:
VUER_CLIENT_URI,WEBSOCKET_MAX_SIZE,VUER_SSL_VERIFY - Use
ssl_verify=Falsewhen connecting through tunnels with self-signed certificates (e.g., ngrok TCP tunnels) - You can also pass any
websockets.connect()kwargs (e.g., a customssl=context) through the constructor