Skip to main content

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 connections provide real-time event streaming and bidirectional API communication.

WebSocket Server

Configuration

Configure NapCat to accept WebSocket connections:

Configuration Options

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

Server Implementation

Create a WebSocket server to receive connections from NapCat:

Authentication

Authorization Header

NapCat sends the token in the Authorization header:

Query Parameter

Or check the access_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 on heartInterval:

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 reconnectInterval milliseconds (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 ws library for WebSocket implementation
  • Supports JSON5 for relaxed message parsing

Best Practices

  1. Use authentication - Always set a token in production
  2. Handle reconnections - Implement reconnection logic in your client
  3. Monitor heartbeats - Use heartbeat events to detect connection issues
  4. Set appropriate intervals - Balance between responsiveness and network overhead
  5. Handle lifecycle events - Track connection state changes
  6. Use echo parameters - Track API request/response pairs
  7. 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 reportSelfMessage setting if expecting self-messages
  • Check that the connection is established (look for lifecycle event)

Frequent Disconnections

  • Check network stability
  • Increase heartInterval to reduce overhead
  • Monitor server logs for error messages

Next Steps