Node.js Tutorial: Installing Socket.IO and Understanding Real-Time Communication

Welcome.
Last time, we created a chat interface (UI) using HTML and CSS.
Now that the interface is ready, it's still just a static page.
When users type messages, nothing happens yet.
This lesson begins our exploration of real-time communication.
We'll install Socket.IO, a crucial technology for chat services, and understand how servers and browsers exchange data in real time.
What is Real-Time Communication?
In typical websites, users must click buttons or refresh pages to see new data.
For example, on a forum, you need to reload the page to see new posts.
But chat services work differently.
When someone sends a message, it appears instantly without refreshing the screen.
This method—where servers and browsers maintain constant connections to exchange data immediately—is called real-time communication.
What is WebSocket?
The leading technology enabling real-time communication is WebSocket.
Traditional HTTP communication closes the connection after each request-response cycle.
In contrast, WebSocket maintains a persistent connection between server and browser once established.
This means:
Server → Browser
Browser → Server
Both sides can send data anytime.
This approach is known as bidirectional communication.
Why Use Socket.IO?
WebSocket alone enables real-time communication, but building a service with it requires handling various challenges:
Browser compatibility
Automatic reconnection when disconnected
Event management
Support for multiple communication methods
Socket.IO handles most of these automatically, making it the most widely used real-time library in Node.js projects.
Installing Socket.IO
First, open your terminal in your project folder.
Enter this command:
npm install socket.io
After installation completes, Socket.IO will appear under dependencies in package.json.
Your project is now ready to use Socket.IO.
Connecting Socket.IO with Express Server
Socket.IO works alongside an existing Express server.
Express serves web pages,
and Socket.IO handles real-time data transmission.
In other words:
Express → Serves web pages
Socket.IO → Handles real-time messaging
This structure is the foundation for all future lessons.
Creating a Socket.IO Server
After setting up your Express server, connect Socket.IO to it.
Your server will now handle both web pages and real-time events.
This structure is the basic setup we'll use throughout this course.
Connecting Socket.IO from the Client Side
Browsers must also connect to the Socket.IO server.
When a webpage loads, it automatically attempts to connect to the server.
If successful, the browser and server maintain a persistent connection.
This connection enables features like:
Chat messages
Connection notifications
User lists
Image sharing
All these will function through this real-time channel.
What are Events?
Socket.IO operates around events.
For example:
User connection
Message sending
Joining a chat room
File uploads
All actions are processed as events.
Understanding events is crucial for web development, so it's important to master this concept.
What is emit()?
The function used to send data in Socket.IO is emit().
For instance, when a user sends a message:
The browser sends an event to the server.
Or,
The server can broadcast new messages to all users.
In short,
emit() is the function for sending data.
What is on()?
The function for receiving data is on().
Browsers or servers wait for specific events to occur, then execute corresponding code.
In short,
on() is the function that waits for events.
Socket.IO always uses emit() and on() together.
Checking Connections
When a browser connects to the server, the server can detect new user connections.
Future features will manage:
Who is connected
How many users are online
Who has disconnected
All using this connection information.
Features to Implement Next
Today we've set up the connection, but future lessons will build on this foundation for various features:
Real-time chat
Nickname settings
User lists
Multiple chat rooms (Rooms)
Private messaging
Image sharing
File sharing
Admin features
All these will operate on Socket.IO's foundation.
Therefore, this lesson is the most important foundational step for your project.
Summary of This Lesson
This session covered installing Socket.IO and understanding how real-time communication works.
We also explored WebSocket vs. Socket.IO differences, and concepts like events, emit(), and on().
While we haven't implemented chat functionality yet, the essential groundwork for real-time communication is complete.
Next Lesson Preview
In the next lesson, we'll build our first real-time chat feature using Socket.IO.
We'll create a system where messages typed in one browser are instantly delivered to other users through the server.
FAQ
What is the difference between WebSocket and Socket.IO?
WebSocket provides basic bidirectional communication, while Socket.IO adds features like automatic reconnection, event management, and browser compatibility handling.
Why is Socket.IO popular for real-time applications?
Socket.IO handles common challenges in real-time communication automatically (like reconnecting after disconnections), making it easier to build robust applications compared to using raw WebSocket.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 강좌] 4강 Socket.IO 설치와 실시간 통신 이해하기