Digging Deeper
Broadcasting
Introduction
Broadcasting in JCC Express MVC is event-driven and powered by Socket.IO. The framework creates a shared socketIo server and routes broadcastable events through the global emit(...) helper.
A dispatched event is treated as a broadcast event when it exposes broadcastOn() (and optionally broadcastWith()).
How broadcasting works
emit(new SomeEvent(...)) calls the framework Event.dispatch.
- If the event has
broadcastOn, the event is sent over Socket.IO. - Event name is the class name (
SomeEvent). - Payload is
broadcastWith()when provided, otherwise the event instance. - Channel behavior:
broadcastOn(): "room"->io.to("room").emit(eventName, payload)broadcastOn(): ["roomA", "roomB"]-> emits to each room- falsy channel -> falls back to
io.emit(...)(broadcast to all)
Define a broadcast event
Implement broadcastOn() (and optionally broadcastWith()) on your event class:
You can also return multiple channels:
Dispatch from controllers or services
emit(...) is a global helper registered at application boot, so it is available during app runtime and in tinker.
Automatic initialization
Socket.IO is initialized automatically when the app boots — no manual setup is required. It attaches to the same HTTP server as Express.
Default CORS configuration:
For production, restrict the origin inside your SocketProvider using the io instance directly, or configure it at the network/proxy level.
Socket provider setup
The framework initializes Socket.IO in the Express layer. For custom socket listeners/rooms, add a provider that extends SocketProvider:
Register the provider in bootstrap/providers.ts.
TypeScript type
When you need to type the Socket.IO server instance in custom code, use the Server type from the socket.io package:
Client-side listening
Listen for the emitted class-name event:
If you emit to a room/channel, ensure the client joined that room first using your socket flow.
Summary
- Broadcasting is just
emit(...)+ event classes withbroadcastOn(). - Event class name becomes the Socket.IO event name.
broadcastWith()controls the payload shape.- Use
SocketProviderfor connection lifecycle and room logic. - Register providers in
bootstrap/providers.ts.