Hello everyone!
We are happy to announce that we now provide a low-level engine for Bun.
Socket.IO is a JavaScript library that enables real-time, bidirectional, and event-based communication between a client and a server. It is commonly used for building applications that require low-latency updates, such as chat applications, collaborative tools, or multiplayer games.
To achieve this, it automatically selects the best available low-level transport between WebTransport, WebSocket and HTTP long-polling, based on the capabilities of the client platform and the network.
Bun is a fast all-in-one JavaScript runtime developed as a modern alternative to Node.js and Deno. It is designed to offer high performance, better developer experience, and compatibility with existing JavaScript/TypeScript projects.
Reference: https://bun.sh/
Bun could already be used without any modification:
import{Server}from"socket.io";
const io =newServer({/* options */});
io.on("connection",(socket)=>{
// ...
});
io.listen(3000);
However, this relied on Bun's polyfill for the Node.js HTTP server.
So, in order to fully benefit from the performance of Bun's native HTTP server, we have created a dedicated engine:
import{ServerasEngine}from"@socket.io/bun-engine";
import{Server}from"socket.io";
const io =newServer();
const engine =newEngine({
path:"/socket.io/",
});
io.bind(engine);
io.on("connection",(socket)=>{
// ...
});
exportdefault{
port:3000,
idleTimeout:30,// must be greater than the "pingInterval" option of the engine, which defaults to 25 seconds
...engine.handler(),
};
Which means that you can now take advantage of the speed and scalability of Bun, with the same high-level API offered by Socket.IO (and its existing adapters, when scaling to multiple servers).
The source code of this engine can be found here: https://github.com/socketio/bun-engine
This new engine can also be used in conjunction with Hono:
import{Server}from"socket.io";
import{ServerasEngine}from"@socket.io/bun-engine";
import{Hono}from"hono";
const io =newServer();
const engine =newEngine();
io.bind(engine);
io.on("connection",(socket)=>{
// ...
});
const app =newHono();
const{ websocket }= engine.handler();
exportdefault{
port:3000,
idleTimeout:30,// must be greater than the "pingInterval" option of the engine, which defaults to 25 seconds
fetch(req, server){
const url =newURL(req.url);
if(url.pathname==="/socket.io/"){
return engine.handleRequest(req, server);
}else{
return app.fetch(req, server);
}
},
websocket
}
Reference: https://hono.dev/docs/
Have a great day!