WebSocket
server.WebSocketConfig controls the availability, limits and resources of WebSocket connections.
The scaffold starts with Enabled: false. Change it to true and start or recharge the server creates websocket/index.gox If missing. An existing file is stored, and disable WebSocket does not remove project code.
var Config = server.WebSocketConfig{
Enabled: true,
HandshakeTimeout: 10 * time.Second,
MaxMessageSize: 8 << 20,
WriteTimeout: 10 * time.Second,
Heartbeat: server.WebSocketHeartbeatConfig{
PingInterval: 25 * time.Second,
PongTimeout: 60 * time.Second,
},
Queue: server.WebSocketQueueConfig{
MaxMessages: 256,
MaxBytes: 16 << 20,
},
Buffers: server.WebSocketBufferConfig{
ReadSize: 4096,
WriteSize: 4096,
},
Compression: server.WebSocketCompressionConfig{
Enabled: false,
Level: 1,
},
} Fields
| Area | Fields |
|---|---|
| Connection | Enabled, HandshakeTimeout, MaxMessageSize, WriteTimeout |
| Heartbeat | PingInterval, PongTimeout |
| Cola | MaxMessages, MaxBytes |
| Buffers | ReadSize, WriteSize |
| Compression | Enabled, Level |
MaxMessageSize limits the messages received and also informs the maximum size to the cluster adapter. The buffers belong to the transport; the tail limits the outstanding messages and their total size.
The values shown are those generated by gox createproject. Zero values do not automatically amount to those explicit values.
The API that consumes this configuration is documented in Socket, and their connection and disconnection events in WebSocket Life Cycle. When there are several instances, Cluster coordinates remote connections and uses MaxMessageSize.
Reloading
Enabled It is immediately applied to new handshakes. The rest of the configuration is consulted when creating each connection, so the sockets open after hot relay use the new values.
The connections that were already open retain the limits and times captured when they were established; hot relaad does not disconnect or reconstruct them. To apply the configuration to all connections, they must reconnect or restart the instance.
Change MaxMessageSize also reopens the cluster runtime, because that limit is part of its configuration.
Best practices
- Define limits in accordance with the actual size of the events.
- Keep a safe difference between ping and pong timeout.
- Toothtail to avoid uncontrolled growth.
- Enable compression only after measuring your cost.