-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(notifications): Live Notifications
- use socket to emit on notification [Finishes #]
- Loading branch information
1 parent
c4c1cad
commit 455fda4
Showing
9 changed files
with
96 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import jwt from 'express-jwt'; | ||
import io from '../index'; | ||
|
||
const socketNotif = notification => { | ||
const onlineUsers = []; | ||
const getLoggedInUser = (token, socket) => { | ||
if (token === null) { | ||
socket.emit('connection_error', 'Token not present'); | ||
} | ||
try { | ||
const user = jwt.verify(token, process.env.JWT_SECRET); | ||
const userObject = { | ||
socketId: socket.id, | ||
userId: user.payload.id, | ||
email: user.payload.email | ||
}; | ||
|
||
const filter = onlineUsers.filter(client => client.userId === user.payload.id); | ||
|
||
if (filter.length === 0) { | ||
onlineUsers.push(userObject); | ||
} else { | ||
onlineUsers.forEach(client => { | ||
if (client.userId === user.payload.id) { | ||
client.socketId = socket.id; | ||
} | ||
}); | ||
} | ||
return user; | ||
} catch (error) { | ||
socket.emit('connection_error', error.message); | ||
} | ||
}; | ||
|
||
try { | ||
io.use((socket, next) => { | ||
const { token } = socket.request._query; | ||
const user = getLoggedInUser(token, socket); | ||
socket.user = user; | ||
next(); | ||
}); | ||
} catch (error) { | ||
socket.emit('connection_error', error.message); | ||
} | ||
|
||
if (!receiverId) { | ||
io.emit(entity, data); | ||
} else if (connectedClients[receiverId.toString()]) { | ||
connectedClients[receiverId.toString()].forEach(element => { | ||
io.to(element).emit(entity, data); | ||
}); | ||
} | ||
}; | ||
|
||
export default socketNotif; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters