Aller au contenu principal

Private messaging - Part III

This guide has four distinct parts:

  • Part I: initial implementation
  • Part II : persistent user ID
  • Part III (current): persistent messages
  • Part IV: scaling up

Here's where we were at the end of the 2nd part:

Chat (v2)

All is working pretty well, but there is a last issue which is quite annoying:

  • when the sender gets disconnected, all the packets it sends are buffered until reconnection (which is great in this case)
Chat with sender that gets disconnected
  • but when the recipient gets disconnected, the packets are lost, since there is no listening Socket instance in the given room
Chat with recipient that gets disconnected

There are multiple solutions to this problem, and we will go for the easiest to implement: store all the messages on the server-side.

Note: this 3rd part will be brief, but it underlines an important property of Socket.IO: you cannot rely on the status of the connection. It should be up most of the time but there is a myriad of things that can kill a TCP connection (this is particularly true on mobile browsers).

Installation​

Let's checkout the branch for part III:

git checkout examples/private-messaging-part-3

Here's what you should see in the current directory:

β”œβ”€β”€ babel.config.js
β”œβ”€β”€ package.json
β”œβ”€β”€ public
β”‚ β”œβ”€β”€ favicon.ico
β”‚ β”œβ”€β”€ fonts
β”‚ β”‚ └── Lato-Regular.ttf
β”‚ └── index.html
β”œβ”€β”€ README.md
β”œβ”€β”€ server
β”‚ β”œβ”€β”€ index.js (updated)
β”‚ β”œβ”€β”€ messageStore.js (created)
β”‚ β”œβ”€β”€ package.json
β”‚ └── sessionStore.js
└── src
β”œβ”€β”€ App.vue
β”œβ”€β”€ components
β”‚ β”œβ”€β”€ Chat.vue (updated)
β”‚ β”œβ”€β”€ MessagePanel.vue
β”‚ β”œβ”€β”€ SelectUsername.vue
β”‚ β”œβ”€β”€ StatusIcon.vue
β”‚ └── User.vue
β”œβ”€β”€ main.js
└── socket.js

The complete diff can be found here.

How it works​

Persistent messages​

On the server-side (server/index.js), we now persist the message in our new store:

io.on("connection", (socket) => {
// ...
socket.on("private message", ({ content, to }) => {
const message = {
content,
from: socket.userID,
to,
};
socket.to(to).to(socket.userID).emit("private message", message);
messageStore.saveMessage(message);
});
// ...
});

And we fetch the list of messages upon connection:

io.on("connection", (socket) => {
// ...
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);
// ...
});

The code is quite straightforward. We shouldn't lose messages upon disconnection anymore:

Chat (v3)

Review​

Now that we have a functional chat, we will see in the 4th part of this guide how to scale to multiple Socket.IO servers.

Thanks for reading!