Implementing Private Messaging (1:1 Chat) in Node.js

Hello.
Last time, we implemented multiple chat rooms where users could only communicate with others in the same room.
Now our chat service resembles real messaging apps more closely.
But one important feature remains to be added:
The private message (whisper) function, allowing messages to be sent exclusively to specific users.
In this lesson, we'll create a 1:1 messaging feature that only the selected user can see, while remaining invisible to others.
What is Private Messaging?
Unlike public chat, private messaging sends messages exclusively to one specific user.
For example:
In public chat:
Chul-su
Hello.
This message would be visible to all users.
But private messaging appears like this:
Chul-su → Young-hee
Let's talk after the meeting.
Only the selected user can see the content.
Where Is This Used?
Private messaging features are common across many services:
Open chat rooms
Game chat
Customer support
Online courses
Admin inquiries
Team collaboration messengers
Most chat services provide personal messaging functionality.
Why Use Socket IDs?
In Socket.IO, each user receives a unique Socket ID.
This allows us to precisely target a single user for message delivery.
For example:
Chul-su
↓
Socket ID
↓
Young-hee
We can use Socket IDs to send data exclusively to the intended recipient.
Selecting a User
To send private messages, you first need to select the recipient.
In this project, we'll implement selecting users from an active user list.
Once selected, all subsequent messages will be sent only to that user.
This mirrors how most messaging apps operate.
Distinguishing Public vs. Private Chat
Users should easily distinguish whether they're sending a public message or private message.
For example, displaying above the input field:
Private Message Target
Young-hee
This helps prevent accidentally sending messages to the wrong person.
Designing Private Messages
Private messages should be visually distinct from regular chat.
For example:
Different colors
Lock icon
PM indicator
Gray background
This makes it easy for users to identify private messages.
Most messaging apps use similar visual distinctions.
How the Server Handles It
When a user sends a private message, the server:
Identifies the sender
Identifies the recipient (via Socket ID)
Sends the message content
The server delivers messages exclusively to the intended user.
Other users won't receive this data.
This is the key difference from public chat.
Senders Should See Their Messages Too
Private messages should appear in both the sender's and receiver's interfaces.
This allows users to verify what they've sent.
For example:
I → Young-hee
Let's talk after the meeting.
This is standard practice in messaging apps.
What About Offline Users?
If the recipient has already disconnected, private messages can't be delivered.
In this case, it's helpful to display:
User is offline
Can't send message
This improves user experience.
For now, our project will only handle active users.
Future Expansion Opportunities
The private messaging feature we've implemented can be expanded with additional functionality:
Private chat rooms
Read receipts
Message deletion
File sharing
Image sharing
Voice messages
View profiles
All of these can be built using the same underlying structure.
Security Matters Too
Private messages must reach only the intended recipient.
The server should verify delivery targets, and clients should display private messages distinctly from public chat to prevent confusion.
This basic security structure is essential in real messaging applications.
Lesson Recap
In this lesson, we implemented a private messaging feature using Socket.IO that sends messages exclusively to specific users.
We also learned how to distinguish users via Socket IDs and visually separate private messages from public chat.
Now our chat service supports both public and private messaging.
Next Lesson Preview
In the next lesson, we'll add emoji support and automatic scrolling features to enhance chat functionality.
We'll implement various user experience improvements to create an interface that closely resembles real messaging apps.
FAQ
Why use Socket IDs for private messaging?
Socket IDs provide a unique identifier for each active user, allowing precise message delivery to specific recipients without relying on usernames or other potentially changeable identifiers.
How should private messages be visually distinguished from public chat?
Private messages can be differentiated using color coding, lock icons, PM indicators, or gray backgrounds to help users easily identify them as personal messages rather than public conversations.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 강좌] 10강 귓속말(1:1 메시지) 기능 구현하기