Teasim WebSocket extends Bun's WebSocket which uses uWebSocket under the hood.
To use websocket, just call ws()
:
Like a normal route, WebSockets also accepts a schema object to strictly type and validate requests.
You can set Teasim constructor to set the Web Socket value.
Teasim's WebSocket implementation extends Bun's WebSocket configuration so if you wish to configure the websocket you can refer to Bun's WebSocket documentation to learn more about this.
Below is a config that extends from Bun WebSocket
@default false
Enable compression for clients that support it.
By default, compression is disabled.
The maximum size of a message.
@default 120
After a connection has not received a message for this many seconds, it will be closed.
@default 16777216
(16MB)
The maximum number of bytes that can be buffered for a single connection.
@default false
Close the connection if the backpressure limit is reached.
Below are the new methods that are available to the WebSocket route
Create a websocket handler
Example:
Type:
endpoint: A path to exposed as websocket handler options: Customize WebSocket handler behavior
WebSocketHandler extends config from config.
Below is a config which is accepted by ws
.
Validatation for an incoming WebSocket request.
It's recommended to use query parameters instead of path parameters in WebSocket, as parsing path parameters is expensive and sometime unrealiable for multiple data with long value.
Callback function for new websocket connection.
Type:
Callback function for incoming websocket message.
Type:
Message
type based on schema.message
. Default is string
.
Callback function for closing websocket connection.
Type:
Callback function for the server is ready to accept more data.
Type:
Parse
middleware to parse the request before upgrading the HTTP connection to WebSocket.
Before Handle
middleware which execute before upgrading the HTTP connection to WebSocket.
Ideal place for validation.
Transform
middleware which execute before validation.
Like transform
, but execute before validation of WebSocket message
Additional headers to add before upgrade connection to WebSocket.
Validate incoming WebSocket message.
By default Teasim will parse incoming stringified JSON message as Object for validation.