WSTun is a WebSocket-based relay server that enables real-time communication between services and users without requiring direct connections. The server acts as a central hub that routes messages between different types of clients.
The WSTun server is an HTTP/WebSocket server that:
- Accepts WebSocket connections from service clients and user clients
- Routes messages between connected clients
- Serves static content (HTML, JS) for built-in services
- Provides REST API for management operations
A Service is a type of application (e.g., fileshare, chat) that can be hosted on the WSTun server. Services are defined by:
- A unique name (e.g.,
fileshare) - HTML/JS files that implement the service logic
- Endpoints (e.g.,
/service,/main)
Services can be:
- Built-in: Pre-installed with the server (fileshare, chat)
- Marketplace: Installed from external marketplace URLs
A Service Instance is a specific running session of a service. For example, a fileshare service can have multiple file-sharing rooms, each being an instance.
Each instance has:
- UUID: Unique identifier (e.g.,
a1b2c3d4) - Name: Human-readable name (e.g., "Team Files")
- Token (optional): Password for joining
- Owner: The service client that created the instance
- Creates and manages service instances
- Receives notifications when users join/leave
- Can kick users from the instance
- Coordinates data between user clients
- Joins an existing service instance
- Sends/receives data through the instance
- Can be kicked by the instance host
┌─────────────────────────────────────────────────────────────────┐
│ WSTun Server │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ HTTP Handler │ │
│ │ - Serves static files (HTML, JS) │ │
│ │ - REST API (/_api/...) │ │
│ │ - WebSocket upgrade │ │
│ └───────────────────────────────────────────────────────────┘ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ WebSocket Handler │ │
│ │ - Message routing │ │
│ │ - Instance management │ │
│ │ - Client registration │ │
│ └───────────────────────────────────────────────────────────┘ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ Service Manager │ │
│ │ - Tracks registered services │ │
│ │ - Manages service instances │ │
│ │ - Handles client connections │ │
│ └───────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│ │ │
│ WebSocket │ WebSocket │ WebSocket
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ Service Client│ │ User Client │ │ User Client │
│ (Instance Host) │ (Member) │ │ (Member) │
│ │ │ │ │ │
│ /fileshare/ │ │ /fileshare/ │ │ /fileshare/ │
│ service │ │ main │ │ main │
└───────────────┘ └───────────────┘ └───────────────┘
Service Client Server
│ │
│──── create_instance ─────>│ (name, token, server_token)
│ │
│<─── instance_created ─────│ (uuid, name)
│ │
User Client Server Service Client
│ │ │
│──── join_instance ───────>│ (uuid, userId, token) │
│ │ │
│ │──── client_connected ───────>│
│ │ │
│<──────── ack ─────────────│ (success, instanceName) │
│ │ │
All data exchange happens through the server. The service client acts as a coordinator:
User Client A Server User Client B
│ │ │
│──── file_register ───────>│ │
│ │───(to service client)───────>│
│ │<──── file_list ──────────────│
│<──── file_list ───────────│───(broadcast to all)────────>│
│ │ │
- Optional server-wide token
- Required for all requests when enabled (except
/libwstun.js) - Passed via
Authorization: Bearer <token>header or?token=<token>query param
- Optional per-instance token
- Set when creating an instance
- Required for users to join that instance
| Endpoint | Description |
|---|---|
/ |
Server info page |
/admin |
Service management UI |
/libwstun.js |
Client library |
/{service}/service |
Service controller page |
/{service}/main |
User client page |
/_api/services |
List installed services |
/_api/instances |
List running instances |
/_api/marketplace/* |
Marketplace operations |
| Message Type | Direction | Description |
|---|---|---|
create_instance |
Client → Server | Create a new instance |
instance_created |
Server → Client | Instance creation result |
join_instance |
Client → Server | Join an existing instance |
list_instances |
Client → Server | List available instances |
instance_list |
Server → Client | List of instances |
kick_client |
Host → Server | Kick a user from instance |
client_connected |
Server → Host | User joined notification |
client_disconnected |
Server → Host | User left notification |
- Server never stores user data: All files, messages, etc. remain on client devices
- Service Manager: Tracks active connections and instances in memory
- Marketplace Service: Stores installed service manifests and files locally
- Use libwstun.js: The client library handles all WebSocket communication and authentication
- Instance-scoped data: Keep data scoped to instances, not globally
- Handle disconnections: Clean up resources when users disconnect
- Broadcast updates: Notify all instance members when data changes