Skip to content

Commit

Permalink
Fix socket notification with multiple user tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Aug 22, 2019
1 parent 916937d commit 1b42d73
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions server/lib/peertube-socket.ts
Expand Up @@ -8,7 +8,7 @@ class PeerTubeSocket {

private static instance: PeerTubeSocket

private userNotificationSockets: { [ userId: number ]: SocketIO.Socket } = {}
private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {}

private constructor () {}

Expand All @@ -22,22 +22,26 @@ class PeerTubeSocket {

logger.debug('User %d connected on the notification system.', userId)

this.userNotificationSockets[userId] = socket
if (!this.userNotificationSockets[userId]) this.userNotificationSockets[userId] = []

this.userNotificationSockets[userId].push(socket)

socket.on('disconnect', () => {
logger.debug('User %d disconnected from SocketIO notifications.', userId)

delete this.userNotificationSockets[userId]
this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket)
})
})
}

sendNotification (userId: number, notification: UserNotificationModel) {
const socket = this.userNotificationSockets[userId]
const sockets = this.userNotificationSockets[userId]

if (!socket) return
if (!sockets) return

socket.emit('new-notification', notification.toFormattedJSON())
for (const socket of sockets) {
socket.emit('new-notification', notification.toFormattedJSON())
}
}

static get Instance () {
Expand Down

0 comments on commit 1b42d73

Please sign in to comment.