A highly efficient, Bun-powered engine for Socket.IO, designed to enhance real-time communication performance.
This package provides a custom engine for Socket.IO that leverages the speed and scalability of Bun to handle WebSocket connections seamlessly.
Reference: https://socket.io/
Table of contents
import { Server as Engine } from "@socket.io/bun-engine";
import { Server } from "socket.io";
const io = new Server();
const engine = new Engine({
path: "/socket.io/",
});
io.bind(engine);
io.on("connection", (socket) => {
// ...
});
export default {
port: 3000,
idleTimeout: 30, // must be greater than the "pingInterval" option of the engine, which defaults to 25 seconds
...engine.handler(),
};
import { Server as Engine } from "@socket.io/bun-engine";
import { Server } from "socket.io";
import { Hono } from "hono";
const io = new Server();
const engine = new Engine();
io.bind(engine);
io.on("connection", (socket) => {
// ...
});
const app = new Hono();
const { websocket } = engine.handler();
export default {
port: 3000,
idleTimeout: 30, // must be greater than the "pingInterval" option of the engine, which defaults to 25 seconds
fetch(req, server) {
const url = new URL(req.url);
if (url.pathname === "/socket.io/") {
return engine.handleRequest(req, server);
} else {
return app.fetch(req, server);
}
},
websocket
}
Reference: https://hono.dev/docs/
Default value: /engine.io/
It is the name of the path that is captured on the server side.
Caution! The server and the client values must match (unless you are using a path-rewriting proxy in between).
Example:
const engine = new Server(httpServer, {
path: "/my-custom-path/",
});
Default value: 20000
This value is used in the heartbeat mechanism, which periodically checks if the connection is still alive between the server and the client.
The server sends a ping, and if the client does not answer with a pong within
pingTimeout
ms, the server considers that the connection is closed.
Similarly, if the client does not receive a ping from the server within
pingInterval + pingTimeout
ms, the client also considers that the connection
is closed.
Default value: 25000
See pingTimeout
for more explanation.
Default value: 10000
This is the delay in milliseconds before an uncompleted transport upgrade is cancelled.
Default value: 1e6
(1 MB)
This defines how many bytes a single message can be, before closing the socket. You may increase or decrease this value depending on your needs.
Default value: -
A function that receives a given handshake or upgrade request as its first parameter, and can decide whether to continue or not.
Example:
const engine = new Server({
allowRequest: (req, connInfo) => {
return Promise.reject("thou shall not pass");
},
});
Default value: -
A set of options related to Cross-Origin Resource Sharing (CORS).
Example:
const engine = new Server({
cors: {
origin: ["https://example.com"],
allowedHeaders: ["my-header"],
credentials: true,
},
});
Default value: -
A function that allows to edit the response headers of the handshake request.
Example:
const engine = new Server({
editHandshakeHeaders: (responseHeaders, req, connInfo) => {
responseHeaders.set("set-cookie", "sid=1234");
},
});
Default value: -
A function that allows to edit the response headers of all requests.
Example:
const engine = new Server({
editResponseHeaders: (responseHeaders, req, connInfo) => {
responseHeaders.set("my-header", "abcd");
},
});