Overview
NapCat supports WebSocket connections in two modes:- WebSocket Server - NapCat listens for incoming connections
- WebSocket Client (Reverse WebSocket) - NapCat connects to your server
WebSocket Server
Configuration
Configure NapCat to accept WebSocket connections:Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
enable | boolean | false | Enable the WebSocket server |
host | string | 127.0.0.1 | Host address to bind |
port | number | 3001 | Port to listen on |
token | string | "" | Access token for authentication |
heartInterval | number | 30000 | Heartbeat interval in milliseconds |
messagePostFormat | string | array | Message format |
reportSelfMessage | boolean | false | Report messages sent by self |
enableForcePushEvent | boolean | true | Force push events |
debug | boolean | false | Enable debug logging |
Connecting to WebSocket Server
Event WebSocket (Receives Events)
API WebSocket (For API Calls)
WebSocket Client (Reverse WebSocket)
Configuration
Configure NapCat to connect to your WebSocket server:Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
enable | boolean | false | Enable reverse WebSocket |
url | string | ws://localhost:8082 | Target WebSocket server URL |
token | string | "" | Access token for authentication |
reconnectInterval | number | 5000 | Reconnection interval in milliseconds |
heartInterval | number | 30000 | Heartbeat interval in milliseconds |
messagePostFormat | string | array | Message format |
reportSelfMessage | boolean | false | Report messages sent by self |
debug | boolean | false | Enable debug logging |
Server Implementation
Create a WebSocket server to receive connections from NapCat:Authentication
Authorization Header
NapCat sends the token in theAuthorization header:
Query Parameter
Or check theaccess_token query parameter:
Events and Lifecycle
Connection Event
When a connection is established, NapCat sends a lifecycle event:Heartbeat Events
NapCat sends periodic heartbeat events based onheartInterval:
Receiving Messages
Message events are pushed automatically:Making API Calls
Through WebSocket Connection
Send API requests through the WebSocket:Response Format
Connection Headers
NapCat sends custom headers when connecting (reverse WebSocket):Automatic Reconnection
For reverse WebSocket, NapCat automatically reconnects if the connection drops:- Reconnection occurs after
reconnectIntervalmilliseconds (default: 5000ms) - Logs show:
在 5 秒后尝试重新连接 - Connection errors trigger reconnection
Multiple Connections
You can configure multiple WebSocket connections:Implementation Details
- WebSocket Server:
packages/napcat-onebot/network/websocket-server.ts - WebSocket Client:
packages/napcat-onebot/network/websocket-client.ts - Maximum payload size: 50MB
- Uses
wslibrary for WebSocket implementation - Supports JSON5 for relaxed message parsing
Best Practices
- Use authentication - Always set a token in production
- Handle reconnections - Implement reconnection logic in your client
- Monitor heartbeats - Use heartbeat events to detect connection issues
- Set appropriate intervals - Balance between responsiveness and network overhead
- Handle lifecycle events - Track connection state changes
- Use echo parameters - Track API request/response pairs
- Implement error handling - Handle WebSocket errors and closures gracefully
Troubleshooting
Connection Refused
- Check that the WebSocket server is enabled and running
- Verify the host and port configuration
- Check firewall settings
Authentication Failed
- Verify the token matches between client and server
- Check that the token is sent in the correct format
- Look for
token验证失败in error messages
No Events Received
- Connect to the root path (
/), not/api - Verify
reportSelfMessagesetting if expecting self-messages - Check that the connection is established (look for lifecycle event)
Frequent Disconnections
- Check network stability
- Increase
heartIntervalto reduce overhead - Monitor server logs for error messages
Next Steps
- Learn about HTTP POST webhooks for one-way event delivery
- Explore HTTP API for request-based communication
- Check troubleshooting guide for common issues
