Usage Chart
How strongly this package shapes the runtime
Package Flow
How this package moves through xtaskjs runtime phases
Before startup
Configure Socket.IO defaults when needed and decorate DI-managed services with SocketGateway plus socket handler decorators so xtaskjs can discover them.
During app.listen()
The integration resolves the live HTTP server from the selected adapter, creates the Socket.IO server, registers namespace tokens, and binds discovered gateways.
During app.close()
The Socket.IO server is closed before the HTTP adapter and container are torn down so namespaces, rooms, and subscriptions stop cleanly.
API Surface
Representative exports from the upstream package
Configuration and tokens
- configureSocketIo
- getSocketIoConfiguration
- getSocketIoLifecycleToken
- getSocketIoServiceToken
- getSocketIoServerToken
- getSocketIoNamespaceToken
Gateways and injectors
- SocketGateway
- OnSocketConnection
- OnSocketDisconnect
- OnSocketEvent
- SubscribeMessage
- InjectSocketService
- InjectSocketLifecycleManager
- InjectSocketServer
- InjectSocketNamespace
Runtime service and types
- SocketIoService
- SocketIoLifecycleManager
- initializeSocketIoIntegration
- shutdownSocketIoIntegration
- getSocketIoLifecycleManager
- resetSocketIoIntegration
- SocketHandlerContext
- SocketGatewaySummary
- SocketEmitOptions
Usage
Typical adoption flow
1. Configure namespace defaults
Call configureSocketIo() when you need a non-root default namespace or custom Socket.IO server options such as CORS and transport settings.
2. Decorate gateways and events
Use SocketGateway on a regular @Service() class, then add OnSocketConnection, OnSocketDisconnect, and OnSocketEvent handlers for each namespace workflow.
3. Broadcast from the rest of the app
Inject SocketIoService, a namespace token, or the raw server when controllers and services need to emit announcements or inspect realtime runtime state.
Example
Reference snippet
import { Service } from "@xtaskjs/core";
import { InjectSocketService, OnSocketEvent, SocketGateway, SocketIoService } from "@xtaskjs/socket-io";
@Service()
@SocketGateway({ namespace: "/chat", group: ["realtime", "chat"] })
export class ChatGateway {
@OnSocketEvent("chat.message")
onMessage(payload: { user: string; text: string }, context: { namespace: any; socket: any }) {
context.namespace.emit("chat.message", {
...payload,
socketId: context.socket.id,
sentAt: new Date().toISOString(),
});
return { ok: true };
}
}
@Service()
export class AnnouncementService {
constructor(
@InjectSocketService()
private readonly sockets: SocketIoService
) {}
announce(message: string) {
this.sockets.emit("server.announcement", { message }, { namespace: "/chat", room: "lobby" });
}
}
Samples
Official samples to inspect next
Reference samples: 23-socket_io_express_app
Related
Packages commonly used with this one