Skip to content

Commit

Permalink
chore: socket events
Browse files Browse the repository at this point in the history
  • Loading branch information
guimroque committed Nov 28, 2024
1 parent 66dca93 commit c6df7ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions packages/api/src/modules/notification/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export class NotificationService implements INotificationService {
});

const socketClient = new SocketClient(notification.user_id, API_URL);
socketClient.socket.emit(SocketEvents.DEFAULT, {
socketClient.socket.emit(SocketEvents.NOTIFICATION, {
sessionId: notification.user_id,
to: SocketUsernames.UI,
request_id: undefined,
type: SocketEvents.NOTIFICATION,
type: SocketEvents.NEW_NOTIFICATION,
data: {}
});

Expand Down Expand Up @@ -159,7 +159,7 @@ export class NotificationService implements INotificationService {

for await (const member of members) {
const socketClient = new SocketClient(member.id, API_URL);
socketClient.socket.emit(SocketEvents.DEFAULT, {
socketClient.socket.emit(SocketEvents.NOTIFICATION, {
sessionId: member.id,
to: SocketUsernames.UI,
request_id: undefined,
Expand All @@ -180,7 +180,7 @@ export class NotificationService implements INotificationService {

for await (const member of members) {
const socketClient = new SocketClient(member.id, API_URL);
socketClient.socket.emit(SocketEvents.DEFAULT, {
socketClient.socket.emit(SocketEvents.NOTIFICATION, {
sessionId: member.id,
to: SocketUsernames.UI,
request_id: undefined,
Expand Down Expand Up @@ -224,7 +224,7 @@ export class NotificationService implements INotificationService {
}

const socketClient = new SocketClient(member.id, API_URL);
socketClient.socket.emit(SocketEvents.DEFAULT, {
socketClient.socket.emit(SocketEvents.NOTIFICATION, {
sessionId: member.id,
to: SocketUsernames.UI,
request_id: undefined,
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/socket/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export enum SocketEvents {
CONNECT = 'connection',
DEFAULT = 'message',
NOTIFICATION = 'notification',

NEW_NOTIFICATION = '[NEW_NOTIFICATION]',
TRANSACTION_UPDATE = '[TRANSACTION_UPDATE]',
VAULT_UPDATE = '[VAULT_UPDATE]',
TRANSACTION_UPDATE = '[TRANSACTION]',
VAULT_UPDATE = '[VAULT]',
}

export enum SocketUsernames {
Expand Down
11 changes: 6 additions & 5 deletions packages/socket-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ io.on(SocketEvents.CONNECT, async socket => {
const { sessionId, username, request_id } = socket.handshake.auth
const requestId = request_id === undefined ? '' : request_id

const room = `${sessionId}:${username}${requestId && ':'+requestId}`
const room = `${sessionId}:${username}${requestId && `:${requestId}`}`

socket.data.messageQueue = []
await socket.join(room)
Expand Down Expand Up @@ -86,19 +86,20 @@ io.on(SocketEvents.CONNECT, async socket => {
socket.to(room).emit(SocketEvents.DEFAULT, data)
})

socket.on(SocketEvents.NEW_NOTIFICATION, data => {
socket.on(SocketEvents.NOTIFICATION, data => {
const { sessionId, to } = data
console.log('[SOCKET]: RECEIVED MESSAGE FROM', sessionId, to)
const room = `${sessionId}:${to}`

console.log('[SOCKET]: SEND MESSAGE TO', room)
console.log('[SOCKET]: REAL TIME MESSAGE TO FRONT', {
room, data
})

socket.to(room).emit(SocketEvents.NOTIFICATION, data)
})

socket.on('disconnect', () => {
console.log('Cliente desconectado:', socket.handshake.auth)
socket.disconnect(true)
// biome-ignore lint/complexity/noForEach: <explanation>
socket.rooms.forEach(room => socket.leave(room))
socket.removeAllListeners()
socket.data.messageQueue = []
Expand Down

0 comments on commit c6df7ec

Please sign in to comment.