Skip to content

Commit

Permalink
docs(examples): 3rd part of the "private messaging" example
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Feb 16, 2021
1 parent 992c938 commit 7247b40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
21 changes: 19 additions & 2 deletions examples/private-messaging/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const randomId = () => crypto.randomBytes(8).toString("hex");
const { InMemorySessionStore } = require("./sessionStore");
const sessionStore = new InMemorySessionStore();

const { InMemoryMessageStore } = require("./messageStore");
const messageStore = new InMemoryMessageStore();

io.use((socket, next) => {
const sessionID = socket.handshake.auth.sessionID;
if (sessionID) {
Expand Down Expand Up @@ -51,11 +54,22 @@ io.on("connection", (socket) => {

// fetch existing users
const users = [];
const messagesPerUser = new Map();
messageStore.findMessagesForUser(socket.userID).forEach((message) => {
const { from, to } = message;
const otherUser = socket.userID === from ? to : from;
if (messagesPerUser.has(otherUser)) {
messagesPerUser.get(otherUser).push(message);
} else {
messagesPerUser.set(otherUser, [message]);
}
});
sessionStore.findAllSessions().forEach((session) => {
users.push({
userID: session.userID,
username: session.username,
connected: session.connected,
messages: messagesPerUser.get(session.userID) || [],
});
});
socket.emit("users", users);
Expand All @@ -65,15 +79,18 @@ io.on("connection", (socket) => {
userID: socket.userID,
username: socket.username,
connected: true,
messages: [],
});

// forward the private message to the right recipient (and to other tabs of the sender)
socket.on("private message", ({ content, to }) => {
socket.to(to).to(socket.userID).emit("private message", {
const message = {
content,
from: socket.userID,
to,
});
};
socket.to(to).to(socket.userID).emit("private message", message);
messageStore.saveMessage(message);
});

// notify users upon disconnection
Expand Down
25 changes: 25 additions & 0 deletions examples/private-messaging/server/messageStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* abstract */ class MessageStore {
saveMessage(message) {}
findMessagesForUser(userID) {}
}

class InMemoryMessageStore extends MessageStore {
constructor() {
super();
this.messages = [];
}

saveMessage(message) {
this.messages.push(message);
}

findMessagesForUser(userID) {
return this.messages.filter(
({ from, to }) => from === userID || to === userID
);
}
}

module.exports = {
InMemoryMessageStore,
};
5 changes: 4 additions & 1 deletion examples/private-messaging/src/components/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,19 @@ export default {
});
const initReactiveProperties = (user) => {
user.messages = [];
user.hasNewMessages = false;
};
socket.on("users", (users) => {
users.forEach((user) => {
user.messages.forEach((message) => {
message.fromSelf = message.from === socket.userID;
});
for (let i = 0; i < this.users.length; i++) {
const existingUser = this.users[i];
if (existingUser.userID === user.userID) {
existingUser.connected = user.connected;
existingUser.messages = user.messages;
return;
}
}
Expand Down

0 comments on commit 7247b40

Please sign in to comment.