WebSockets
Build real-time applications with Socket.IO, natively integrated into the LykosJS server and dependency injection system.
Initialize Socket.IO
1const io = app.socketIO({2 cors: {3 origin: 'https://app.example.com',4 },5})6 7io.on('connection', (socket) => {8 socket.emit('ready', { ok: true })9})LykosJS provides built-in Socket.IO integration powered by @socket.io/bun-engine. Calling app.socketIO() creates and binds a Socket.IO server to the Bun engine. The default engine path is /socket.io/.
The integration is initialized only once per application instance. Subsequent calls to socketIO() return the existing Socket.IO server instead of creating another one.
Inject Socket.IO into modules
1app.modules(Modules)When modules are loaded, LykosJS automatically makes the initialized Socket.IO server available through the dependency injection container. You can inject the Socket.IO Server into your providers using its class as the injection token.
1import { Server } from 'socket.io'2 3export class NotificationsService {4 constructor(private readonly io: Server) {}5 6 notify(userId: string, message: string) {7 this.io.to(userId).emit('notification', { message })8 }9}You can also provide an explicit Socket.IO instance through the modules context:
1app.modules(Modules, { io })Start the server
1app.listen(4000)The HTTP server receives the Socket.IO integration automatically when it starts, allowing HTTP routes and real-time connections to run through the same LykosJS application.