method
WebSocketHandler.pong
The websocket that received the pong
The data sent with the pong
Referenced types
interface ServerWebSocket<T = undefined>
A fast WebSocket designed for servers.
Features:
- Message compression - Messages can be compressed
- Backpressure -
send()reports when the client is not ready to receive data. - Dropped messages -
send()reports when the client cannot receive data. - Topics - Messages can be ServerWebSocket.published to a specific topic and the client can ServerWebSocket.subscribe to topics
This differs slightly from the browser WebSocket, which Bun supports for clients.
Powered by uWebSockets.
Bun.serve({
websocket: {
open(ws) {
console.log("Connected", ws.remoteAddress);
},
message(ws, data) {
console.log("Received", data);
ws.send(data);
},
close(ws, code, reason) {
console.log("Disconnected", code, reason);
},
}
});- binaryType?: 'arraybuffer' | 'uint8array' | 'nodebuffer'
Sets how binary data is returned in events.
- if
nodebuffer, binary data is returned asBufferobjects. (default) - if
arraybuffer, binary data is returned asArrayBufferobjects. - if
uint8array, binary data is returned asUint8Arrayobjects.
let ws: WebSocket; ws.binaryType = "uint8array"; ws.addEventListener("message", ({ data }) => { console.log(data instanceof Uint8Array); // true }); - if
- data: T
Custom data you can assign to a client. It can be read and written at any time.
import { serve } from "bun"; serve({ fetch(request, server) { const data = { accessToken: request.headers.get("Authorization"), }; if (server.upgrade(request, { data })) { return; } return new Response(); }, websocket: { data: {} as {accessToken: string | null}, message(ws) { console.log(ws.data.accessToken); } } }); - readonly readyState: WebSocketReadyState
The ready state of the client.
- if
0, the client is connecting. - if
1, the client is connected. - if
2, the client is closing. - if
3, the client is closed.
console.log(socket.readyState); // 1 - if
- readonly remoteAddress: string
The IP address of the client.
console.log(socket.remoteAddress); // "127.0.0.1" - readonly subscriptions: string[]
Returns an array of all topics the client is currently subscribed to.
ws.subscribe("chat"); ws.subscribe("notifications"); console.log(ws.subscriptions); // ["chat", "notifications"] - code?: number,reason?: string): void;
Closes the connection.
Close codes:
1000means "normal closure" (default)1009means a message was too big and was rejected1011means the server encountered an error1012means the server is restarting1013means the server is too busy or the client is rate-limited4000through4999are reserved for applications
To close the connection abruptly, use
terminate().@param codeThe close code to send
@param reasonThe close reason to send
- ): T;
Batches
send()andpublish()operations, which makes it faster to send data.The
message,open, anddraincallbacks are automatically corked, so you only need to call this if you are sending messages outside of those callbacks or in async functions.@param callbackThe callback to run.
ws.cork((ctx) => { ctx.send("These messages"); ctx.sendText("are sent"); ctx.sendBinary(new TextEncoder().encode("together!")); }); - @param topic
The topic name.
ws.subscribe("chat"); console.log(ws.isSubscribed("chat")); // true - @param data
The data to send
- @param data
The data to send
- topic: string,compress?: boolean): number;
Sends a message to subscribers of the topic.
@param topicThe topic name.
@param dataThe data to send.
@param compressWhether to compress the data. Ignored if the client does not support compression
ws.publish("chat", "Hello!"); ws.publish("chat", "Compress this.", true); ws.publish("chat", new Uint8Array([1, 2, 3, 4])); - topic: string,compress?: boolean): number;
Sends a binary message to subscribers of the topic.
@param topicThe topic name.
@param dataThe data to send.
@param compressWhether to compress the data. Ignored if the client does not support compression
ws.publish("chat", new TextEncoder().encode("Hello!")); ws.publish("chat", new Uint8Array([1, 2, 3, 4]), true); - topic: string,data: string,compress?: boolean): number;
Sends a text message to subscribers of the topic.
@param topicThe topic name.
@param dataThe data to send.
@param compressWhether to compress the data. Ignored if the client does not support compression
ws.publish("chat", "Hello!"); ws.publish("chat", "Compress this.", true); - @param data
The data to send.
@param compressWhether to compress the data. Ignored if the client does not support compression
ws.send("Hello!"); ws.send("Compress this.", true); ws.send(new Uint8Array([1, 2, 3, 4])); - compress?: boolean): number;
Sends a binary message to the client.
@param dataThe data to send.
@param compressWhether to compress the data. Ignored if the client does not support compression
ws.send(new TextEncoder().encode("Hello!")); ws.send(new Uint8Array([1, 2, 3, 4]), true); - @param data
The data to send.
@param compressWhether to compress the data. Ignored if the client does not support compression
ws.send("Hello!"); ws.send("Compress this.", true); - @param topic
The topic name.
@returnstrueif the client is now subscribed,falseif the socket is closed.ws.subscribe("chat"); Closes the connection abruptly.
To close the connection gracefully, use
close().- @param topic
The topic name.
@returnstrueif the client was subscribed and is now unsubscribed,falseif the socket is closed or the client was not subscribed to the topic.ws.unsubscribe("chat");