{"id":2366,"date":"2026-07-24T00:37:35","date_gmt":"2026-07-23T17:37:35","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-build-a-real-time-chat-app-with-socket-io-a-step-by-step-guide\/"},"modified":"2026-07-24T00:37:35","modified_gmt":"2026-07-23T17:37:35","slug":"how-to-build-a-real-time-chat-app-with-socket-io-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-build-a-real-time-chat-app-with-socket-io-a-step-by-step-guide\/","title":{"rendered":"How to Build a Real-Time Chat App with Socket.io: A Step-by-Step Guide"},"content":{"rendered":"<h1>How to Build a Real-Time Chat App with Socket.io: A Step-by-Step Guide<\/h1>\n<p>Building a real-time chat application can seem daunting, but Socket.io simplifies the process by handling bidirectional communication between client and server. In this tutorial, you&#8217;ll learn how to set up a basic chat app using Node.js and Socket.io, enabling instant message delivery without page refreshes. We&#8217;ll focus on the core components: server setup, client connection, and real-time messaging.<\/p>\n<p>Before you begin, ensure you have Node.js installed on your machine. We&#8217;ll use Express to serve the HTML file and Socket.io for the WebSocket layer. Create a new project folder, run <code>npm init -y<\/code>, then install both dependencies with <code>npm install express socket.io<\/code>. This gives you everything needed to start coding.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20build%20a%20chat%20app%20with%20Socket.io\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Set Up the Server<\/h2>\n<p>Create a file named <code>server.js<\/code>. Import Express and Socket.io, then initialize them. Listen for connections and handle the <code>connection<\/code> event. Inside the callback, listen for <code>chat message<\/code> events and broadcast them to all connected clients using <code>io.emit()<\/code>. Here&#8217;s a minimal server example:<\/p>\n<ul>\n<li>Import: <code>const express = require('express'); const http = require('http'); const socketIo = require('socket.io');<\/code><\/li>\n<li>Create app and server: <code>const app = express(); const server = http.createServer(app); const io = socketIo(server);<\/code><\/li>\n<li>Serve static files (like <code>index.html<\/code>) with <code>app.use(express.static('public'));<\/code><\/li>\n<li>Listen on port 3000: <code>server.listen(3000, () => console.log('listening on *:3000'));<\/code><\/li>\n<\/ul>\n<h2>2. Create the Client HTML<\/h2>\n<p>Inside a <code>public<\/code> folder, create <code>index.html<\/code>. Include the Socket.io client library via CDN: <code>&lt;script src=\"\/socket.io\/socket.io.js\"&gt;&lt;\/script&gt;<\/code>. Add a simple form with an input field and a messages list. Use JavaScript to connect to the server and handle events: <code>const socket = io();<\/code>.<\/p>\n<p>To send a message, emit a <code>chat message<\/code> event with the input value. To receive messages, listen for the same event on the client and append the data to the messages list.<\/p>\n<h2>3. Implement Real-Time Messaging<\/h2>\n<p>On the server, inside the <code>connection<\/code> callback, add a listener for <code>chat message<\/code>:<\/p>\n<ul>\n<li><code>socket.on('chat message', (msg) => { io.emit('chat message', msg); });<\/code><\/li>\n<\/ul>\n<p>On the client, after connecting, listen for the <code>chat message<\/code> event:<\/p>\n<ul>\n<li><code>socket.on('chat message', (msg) => { const li = document.createElement('li'); li.textContent = msg; document.getElementById('messages').appendChild(li); });<\/code><\/li>\n<\/ul>\n<p>When the form is submitted, prevent default behavior and emit the message: <code>socket.emit('chat message', input.value); input.value = '';<\/code><\/p>\n<p>That&#8217;s it! Run <code>node server.js<\/code> and open <code>http:\/\/localhost:3000<\/code> in multiple browser tabs. You&#8217;ll see messages appear instantly across all clients. This simple foundation can be extended with features like usernames, rooms, and typing indicators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a Real-Time Chat App with Socket.io: A Step-by-Step Guide Building a real-time chat application can seem daunting, but Socket.io simplifies the process by handling bidirectional communication between client and server. In this tutorial, you&#8217;ll learn how to set up a basic chat app using Node.js and Socket.io, enabling instant message delivery without &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2366","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=2366"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2366\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}