Bidirectional RPC — server and client can call each other's methods
Pub/Sub — fire-and-forget notifications via publish / subscribe
Batch requests — multiple calls per frame, results returned in order
Auth — token validated during HTTP upgrade
Heartbeat — ping/pong with auto-disconnect on timeout
Auto-reconnect — exponential backoff, handlers and subscriptions preserved
Broadcast — send to all connections, filter by role, or exclude specific peers
Example
Python
Server
from echorpc import EchoServer
server = EchoServer(port=9100)# Define a command that clients can call@server.command("add")defadd(params):return{"sum": params["a"]+ params["b"]}# Listen for events from clients@server.event("chat")asyncdefon_chat(data):print("on_chat", data)await server.start()
Client
from echorpc import EchoClient
client = EchoClient("ws://localhost:9100")# Let the server call you backclient.register("double",lambda p: p["x"]*2)# Subscribe to eventsclient.subscribe("chat",lambda data:print(data))await client.connect()# Call a server commandresult =await client.request("add",{"a":1,"b":2})# Publish event to the serverawait client.publish("chat",{"text":"hello"})# Send multiple calls at onceresults =await client.batch_request([("add",{"a":1,"b":2}),("add",{"a":3,"b":4}),])
TypeScript
Server
import{ EchoServer }from"echorpc";const server =newEchoServer({ port:9100,authHandler:(p)=> p.token ==="secret",});// Define a command that clients can callserver.register("add",(p:{ a:number; b:number})=>({ sum: p.a + p.b,}));// Listen for events from clientsserver.subscribe("chat",async(data)=>{console.log("chat", data);});await server.start();
Client (Node.js)
import WebSocket from"ws";import{ EchoClient }from"echorpc";const client =newEchoClient("ws://localhost:9100",{ token:"secret", WebSocket,});// Let the server call you backclient.register("double",(p)=> p.x *2);// Subscribe to eventsclient.subscribe("chat",(data)=>console.log(data));await client.connect();// Call a server commandconst result =await client.request("add",{ a:1, b:2});// Publish event to the serverclient.publish("chat",{ text:"hello"});// Send multiple calls at onceconst results =await client.batchRequest([["add",{ a:1, b:2}],["add",{ a:3, b:4}],]);