WebSocket â
WebSocket is a realtime protocol for communication between your client and server.
Unlike HTTP where our client repeatedly asking the website for information and waiting for a reply each time, WebSocket sets up a direct line where our client and server can send messages back and forth directly, making the conversation quicker and smoother without having to start over each message.
SocketIO is a popular library for WebSocket, but it is not the only one. Elysia uses uWebSocket which Bun uses under the hood with the same API.
To use websocket, simply call Elysia.ws()
:
import { Elysia } from 'elysia'
new Elysia()
.ws('/ws', {
message(ws, message) {
ws.send(message)
}
})
.listen(8080)
import { Elysia } from 'elysia'
new Elysia()
.ws('/ws', {
message(ws, message) {
ws.send(message)
}
})
.listen(8080)
WebSocket message validation: â
Same as normal route, WebSockets also accepts a schema object to strictly type and validate requests.
import { Elysia, t } from 'elysia'
const app = new Elysia()
.ws('/ws', {
// validate incoming message
body: t.Object({
message: t.String()
}),
message(ws, { message }) {
ws.send({
message,
time: Date.now()
})
}
})
.listen(8080)
import { Elysia, t } from 'elysia'
const app = new Elysia()
.ws('/ws', {
// validate incoming message
body: t.Object({
message: t.String()
}),
message(ws, { message }) {
ws.send({
message,
time: Date.now()
})
}
})
.listen(8080)
WebSocket schema can validate the following:
- message - An incoming message.
- query - query string or URL parameters.
- params - Path parameters.
- header - Request's headers.
- cookie - Request's cookie
- response - Value returned from handler
By default Elysia will parse incoming stringified JSON message as Object for validation.
Configuration â
You can set Elysia constructor to set the Web Socket value.
new Elysia({
websocket: {
idleTimeout: 30
}
})
new Elysia({
websocket: {
idleTimeout: 30
}
})
Elysia's WebSocket implementation extends Bun's WebSocket configuration, please refer to Bun's WebSocket documentation for more information.
The following are a brief configuration from Bun WebSocket
perMessageDeflate â
@default false
Enable compression for clients that support it.
By default, compression is disabled.
maxPayloadLength â
The maximum size of a message.
idleTimeout â
@default 120
After a connection has not received a message for this many seconds, it will be closed.
backpressureLimit â
@default 16777216
(16MB)
The maximum number of bytes that can be buffered for a single connection.
closeOnBackpressureLimit â
@default false
Close the connection if the backpressure limit is reached.
Methods â
Below are the new methods that are available to the WebSocket route
ws â
Create a websocket handler
Example:
import { Elysia } from 'elysia'
const app = new Elysia()
.ws('/ws', {
message(ws, message) {
ws.send(message)
}
})
.listen(8080)
import { Elysia } from 'elysia'
const app = new Elysia()
.ws('/ws', {
message(ws, message) {
ws.send(message)
}
})
.listen(8080)
Type:
.ws(endpoint: path, options: Partial<WebSocketHandler<Context>>): this
.ws(endpoint: path, options: Partial<WebSocketHandler<Context>>): this
endpoint: A path to exposed as websocket handler options: Customize WebSocket handler behavior
WebSocketHandler â
WebSocketHandler extends config from config.
Below is a config which is accepted by ws
.
open â
Callback function for new websocket connection.
Type:
open(ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>): this
open(ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>): this
message â
Callback function for incoming websocket message.
Type:
message(
ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>,
message: Message
): this
message(
ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>,
message: Message
): this
Message
type based on schema.message
. Default is string
.
close â
Callback function for closing websocket connection.
Type:
close(ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>): this
close(ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>): this
drain â
Callback function for the server is ready to accept more data.
Type:
drain(
ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>,
code: number,
reason: string
): this
drain(
ws: ServerWebSocket<{
// uid for each connection
id: string
data: Context
}>,
code: number,
reason: string
): this
parse â
Parse
middleware to parse the request before upgrading the HTTP connection to WebSocket.
beforeHandle â
Before Handle
middleware which execute before upgrading the HTTP connection to WebSocket.
Ideal place for validation.
transform â
Transform
middleware which execute before validation.
transformMessage â
Like transform
, but execute before validation of WebSocket message
header â
Additional headers to add before upgrade connection to WebSocket.