fix: broadcasting received events to all workers

This commit is contained in:
Ricardo Arturo Cabral Mejia 2022-10-14 03:12:48 +00:00
parent 13bd023a1b
commit 0dc7e8352e
No known key found for this signature in database
GPG Key ID: 5931EBF43A650245
2 changed files with 18 additions and 0 deletions

View File

@ -60,6 +60,10 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
public onBroadcast(event: Event): void {
this.webSocketServer.emit(WebSocketServerAdapterEvent.Broadcast, event)
process.send({
eventName: WebSocketServerAdapterEvent.Broadcast,
event,
})
}
public onSendEvent(event: Event): void {

View File

@ -45,6 +45,16 @@ if (cluster.isPrimary) {
process.exit(0)
}
cluster.on('message', (source, message) => {
for (const worker of Object.values(cluster.workers)) {
if (source.id === worker.id) {
continue
}
worker.send(message)
}
})
process.on('SIGINT', exitHandler)
process.on('uncaughtException', exitHandler)
} else if (cluster.isWorker) {
@ -76,5 +86,9 @@ if (cluster.isPrimary) {
process.on('SIGINT', exitHandler)
process.on('uncaughtException', exitHandler)
process.on('message', (message: { eventName: string, event: unknown }) => {
adapter.emit(message.eventName, message.event)
})
console.log(`worker ${process.pid} - listening on port ${port}`)
}