{"id":46792,"date":"2024-02-28T00:00:00","date_gmt":"2024-02-28T08:00:00","guid":{"rendered":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/"},"modified":"2025-11-13T12:56:53","modified_gmt":"2025-11-13T20:56:53","slug":"how-to-build-a-real-time-chat-application-with-node-js-and-griddb","status":"publish","type":"post","link":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/","title":{"rendered":"How to build a Real-time Chat Application with Node.js and GridDB"},"content":{"rendered":"<h2>Introduction:<\/h2>\n<p>In this tutorial, we&#8217;re going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with friends, family, or colleagues from anywhere in the world. That&#8217;s the power of real-time chat, and today, you&#8217;re going to learn how to build one from scratch!<\/p>\n<p>We&#8217;ll be using Node.js, a popular JavaScript runtime, to handle the server-side operations of our chat app. For the database, we&#8217;re going to utilize GridDB, a highly efficient NoSQL database that&#8217;s perfect for handling real-time data processing. Alongside these, we&#8217;ll incorporate Express, a minimalist web framework for Node.js, and Socket.io, a library that enables real-time, bidirectional event-based communication.<\/p>\n<p>This tutorial is designed for those who have a basic understanding of JavaScript and are familiar with Node.js. Don&#8217;t worry if you&#8217;re not an expert; we&#8217;ll guide you through each step clearly and concisely. By the end of this tutorial, you&#8217;ll have a fully functioning real-time chat application, a deeper understanding of how these technologies work together, and a foundation you can use to build more complex applications in the future. Let&#8217;s get started!<\/p>\n<h2>Understanding the Technologies<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&amp;id=14eOM6_-nxub982K67PJ79kqfb13JzPKA\" alt=\"\" \/><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/img2.png\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/img2.png\" alt=\"\" width=\"654\" height=\"360\" class=\"aligncenter size-full wp-image-29996\" srcset=\"\/wp-content\/uploads\/2024\/02\/img2.png 654w, \/wp-content\/uploads\/2024\/02\/img2-300x165.png 300w, \/wp-content\/uploads\/2024\/02\/img2-600x330.png 600w\" sizes=\"(max-width: 654px) 100vw, 654px\" \/><\/a><\/p>\n<h4>NodeJS:<\/h4>\n<p>Node.js functions as a runtime environment that enables JavaScript code to operate outside the confines of a conventional web browser. Real-time application management is made easy using Node.js&#8217;s non-blocking, event-driven design, which leverages the Chrome V8 JavaScript engine. Its emphasis is on efficiency and responsiveness.<\/p>\n<h4>GridDB:<\/h4>\n<p>GridDB is particularly effective in real-time Internet of Things applications. It guarantees dependable and quick access with minimal latency and effective data management. GridDB is a great tool for efficient data management in chat apps since it is designed for real-time processing.<\/p>\n<h4>Express:<\/h4>\n<p>Express is a Node.js web application framework that offers numerous features for creating mobile and web applications. It is employed to build single-page, multipage, and hybrid web applications. As an overlay on top of Node.js, Express streamlines server and route management<\/p>\n<h4>Socket.io:<\/h4>\n<p>A library called Socket.io is intended for real-time, event-driven web applications. It allows two-way communication between web clients and servers.<\/p>\n<h2>Prerequisites:<\/h2>\n<ul>\n<li>It is essential to have a basic grasp of JavaScript.<\/li>\n<li>Make sure that Node and NPM are installed on your computer.<\/li>\n<\/ul>\n<h4>Step-by-Step configuration:<\/h4>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/img3.png\"><img decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/img3.png\" alt=\"\" width=\"654\" height=\"360\" class=\"aligncenter size-full wp-image-29997\" srcset=\"\/wp-content\/uploads\/2024\/02\/img3.png 654w, \/wp-content\/uploads\/2024\/02\/img3-300x165.png 300w, \/wp-content\/uploads\/2024\/02\/img3-600x330.png 600w\" sizes=\"(max-width: 654px) 100vw, 654px\" \/><\/a><br \/>\nCreate a folder by using the command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">mkdir folder_name<\/code><\/pre>\n<\/div>\n<p>Change the current working directory by using the command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">cd folder_name<\/code><\/pre>\n<\/div>\n<p>After that, open the folder you just created in Visual Studio Code, and to launch a new project, run the command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">npm init -y<\/code><\/pre>\n<\/div>\n<p>The output will look like this:<\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/img4.png\"><img decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/img4.png\" alt=\"\" width=\"657\" height=\"210\" class=\"aligncenter size-full wp-image-29998\" srcset=\"\/wp-content\/uploads\/2024\/02\/img4.png 657w, \/wp-content\/uploads\/2024\/02\/img4-300x96.png 300w, \/wp-content\/uploads\/2024\/02\/img4-600x192.png 600w\" sizes=\"(max-width: 657px) 100vw, 657px\" \/><\/a><\/p>\n<p>Now, to install Express, use the command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">npm install -save express<\/code><\/pre>\n<\/div>\n<p>Now install socket.io using the command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">npm install -save socket.io<\/code><\/pre>\n<\/div>\n<h4>Setting Up GridDB:<\/h4>\n<p><!-- ![](https:\/\/drive.google.com\/uc?export=view&id=1_zOCAtzyIUjews2xtiFz-ipyrLV_VGU5) --><\/p>\n<p>Run the following commands on the system\u2019s terminal.<\/p>\n<p>To install with rpm, run the commands:<\/p>\n<p>First, create the apt repo file by using:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">sudo sh -c 'echo \"deb https:\/\/www.griddb.net\/apt griddb\/5.3 multiverse\" >>  \/etc\/apt\/sources.list.d\/griddb.list'<\/code><\/pre>\n<\/div>\n<p>Then import the key by using:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">wget -qO - https:\/\/www.griddb.net\/apt\/griddb.asc | sudo apt-key add -<\/code><\/pre>\n<\/div>\n<p>Next, check for the updates using:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">sudo apt update<\/code><\/pre>\n<\/div>\n<p>Finally, install GridDB by using:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">sudo apt install griddb-meta<\/code><\/pre>\n<\/div>\n<p>Start the GridDB Server by using:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">sudo systemctl start gridstore<\/code><\/pre>\n<\/div>\n<p>Stop the server by using:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">sudo systemctl stop gridstore<\/code><\/pre>\n<\/div>\n<p>Change the default password of admin by using the command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">export GS_HOME=\/var\/lib\/gridstore\/\nexport GS_LOG=\/var\/lib\/gridstore\/log\/\ngs_passwd admin\n<\/code><\/pre>\n<\/div>\n<p>Due to the absence of an init script in the GridDB Community Edition, GridDB must be started manually. Establish the service&#8217;s connection to other nodes after it has been started.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">gs_startnode -u username\/password\ngs_joincluster -u admin\/admin<\/code><\/pre>\n<\/div>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/start_node.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/start_node.png\" alt=\"\" width=\"712\" height=\"246\" class=\"aligncenter size-full wp-image-30003\" srcset=\"\/wp-content\/uploads\/2024\/02\/start_node.png 712w, \/wp-content\/uploads\/2024\/02\/start_node-300x104.png 300w, \/wp-content\/uploads\/2024\/02\/start_node-600x207.png 600w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/a><br \/>\nYou can verify the status of GridDB by using the command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">gs_stat -u admin\/admin<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/gs_stat.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/gs_stat.png\" alt=\"\" width=\"1368\" height=\"768\" class=\"aligncenter size-full wp-image-29994\" srcset=\"\/wp-content\/uploads\/2024\/02\/gs_stat.png 1368w, \/wp-content\/uploads\/2024\/02\/gs_stat-300x168.png 300w, \/wp-content\/uploads\/2024\/02\/gs_stat-1024x575.png 1024w, \/wp-content\/uploads\/2024\/02\/gs_stat-768x431.png 768w, \/wp-content\/uploads\/2024\/02\/gs_stat-150x85.png 150w, \/wp-content\/uploads\/2024\/02\/gs_stat-600x337.png 600w\" sizes=\"(max-width: 1368px) 100vw, 1368px\" \/><\/a><\/p>\n<p><!-- https:\/\/drive.google.com\/file\/d\/1bdCCLt1zSAkTd1vaWRFNBWS5J_irQlUW\/view?usp=sharing --><\/p>\n<p>Then, create a file (in Visual Studio Code) called server.js that Node will use to function as the server.<\/p>\n<p><strong>Add the following code to it:<\/strong><\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-js\">const express = require(\"express\");\nconst socketio = require(\"socket.io\");\nconst http = require(\"http\");\nconst app = express();\nconst griddb = require(\"griddb-node-api\");\n\nconst PORT = 3000;\nconst HOST = \"0.0.0.0\";\nconst server = http.createServer(app);\nconst io = socketio(server);\nconst factory = griddb.StoreFactory.getInstance();\nconst store = factory.getStore({\n  notificationMember: \"127.0.0.1:10001\",\n  clusterName: \"myCluster\",\n  username: \"admin\",\n  password: \"admin\",\n});\n\n\/\/ Schema\nvar timeConInfo = new griddb.ContainerInfo({\n  name: \"Chat\",\n  columnInfoList: [\n    [\"timestamp\", griddb.Type.TIMESTAMP],\n    [\"username\", griddb.Type.STRING],\n    [\"message\", griddb.Type.STRING],\n  ],\n  type: griddb.ContainerType.TIME_SERIES,\n  rowKey: true,\n});\n\n\/\/ Function to handle the \"chat_message\" event and store the message in GridDB\nasync function handleChatMessage(data) {\n  try {\n    let time_series;\n    store\n      .putContainer(timeConInfo, false)\n      .then((ts) => {\n        time_series = ts;\n        return ts.put([new Date(), data.username, data.message]);\n      })\n      .then(() => {\n        query = time_series.query(\n          \"select * where timestamp > TIMESTAMPADD(HOUR, NOW(), -6)\"\n        );\n        return query.fetch();\n      })\n      .then((rowset) => {\n        while (rowset.hasNext()) {\n          var row = rowset.next();\n          console.log(\n            \"Time =\",\n            row[0],\n            \"User =\",\n            row[1].toString(),\n            \"Text =\",\n            row[2]\n          );\n        }\n      })\n      .catch((err) => {\n        if (err.constructor.name == \"GSException\") {\n          for (var i = 0; i < err.getErrorStackSize(); i++) {\n            console.log(\"[\", i, \"]\");\n            console.log(err.getErrorCode(i));\n            console.log(err.getMessage(i));\n          }\n        } else {\n          console.log(err);\n        }\n      });\n\n    console.log(\"Chat message stored successfully.\");\n  } catch (error) {\n    console.error(\"Error storing chat message:\", error);\n  }\n}\n\n\/\/ Fetch all data\nconst FetchAll = async function () {\n  const container = await store.getContainer(\"Chat\");\n\n  const query = container.query(\n    \"select * where timestamp > TIMESTAMPADD(HOUR, NOW(), -6)\"\n  );\n  const rowSet = await query.fetch();\n  let res = [];\n  while (rowSet.hasNext()) {\n    const row = await rowSet.next();\n    console.log(\"Name:\", row[1].toString(), \"Message:\", row[2]);\n    res.push({ username: row[1].toString(), message: row[2] });\n  }\n  return res;\n};\n\napp.use(express.static(__dirname + \"\/public\"));\n\napp.get(\"\/\", (req, res) => {\n  res.sendFile(__dirname + \"\/public\/index.html\");\n});\n\nio.on(\"connection\", (socket) => {\n  console.log(\"A user connected: \", socket.id);\n\n  socket.on(\"user_join\", (username) => {\n    io.emit(\"user_join\", username);\n  });\n\n  socket.on(\"chat_message\", (data) => {\n    io.emit(\"chat_message\", data);\n    handleChatMessage(data);\n  });\n\n  socket.on(\"disconnect\", () => {\n    console.log(\"User disconnected\");\n  });\n});\n\napp.get(\"\/\", (req, res) => {\n  res.send(\"Hello, World\");\n});\n\napp.get(\"\/api\/messages\", async (req, res) => {\n  const response = await FetchAll();\n  res.send(response);\n});\n\napp.post(\"\/api\/put\", async (req, res) => {\n  const { username, message } = req.query;\n  let data = {\n    username,\n    message,\n  };\n  handleChatMessage(data);\n\n  res.send(\"done\");\n});\n\nserver.listen(PORT, HOST);\nconsole.log(`Running on http:\/\/${HOST}:${PORT}`);<\/code><\/pre>\n<\/div>\n<h3>Explanation of the code:<\/h3>\n<p>The above code creates a real-time chat server using Express, Socket.io, and GridDB. It connects to GridDB to store and retrieve chat messages in a time-series container with timestamp, username, and message columns. The server records user connections, disconnections, and chat messages. The <code>handleChatMessage<\/code> function asynchronously stores chat messages in GridDB. The server uses Express for the web interface and Socket-io for real-time communication. The code effectively integrates these technologies for a comprehensive chat server.<\/p>\n<h3>GridDB Initialization and Configuration<\/h3>\n<p>GridDB Store Initialization:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">const factory = griddb.StoreFactory.getInstance();\nconst store = factory.getStore({...});<\/code><\/pre>\n<\/div>\n<p>These lines initialize the connection to the GridDB store. <code>StoreFactory.getInstance()<\/code> fetches an instance of the store factory, and getStore() is then used to create a store connection with configuration details such as the cluster name, notification member (server address), and credentials.<\/p>\n<h4>Defining the Chat Container Schema:<\/h4>\n<p><code>var timeConInfo = new griddb.ContainerInfo({...});<\/code><br \/>\nThis section defines the schema for a GridDB container named <code>Chat<\/code>. Containers in GridDB are similar to tables in relational databases. The schema specifies the data columns: a timestamp, a username, and a message, which are essential for a chat application. The type of the container is set to <code>TIME_SERIES<\/code>, indicating that the data is time-sequenced, which is apt for chat messages.<\/p>\n<h4>Creating\/Accessing the Chat Container and Initial Data Insertion:<\/h4>\n<p>The <code>store.putContainer()<\/code> method is used to either create a new container or access an existing one based on the provided schema. The code inserts a sample record into the time series container.<\/p>\n<h4>GridDB Data Handling in Chat Application<\/h4>\n<p><strong>Querying Recent Messages:<\/strong><\/p>\n<p>The code includes a query to retrieve messages from the last six hours. This is done using <code>time_series.query()<\/code>, demonstrating how GridDB can efficiently handle time-based queries, a common requirement in chat applications for fetching recent messages.<\/p>\n<p><strong>Inserting Chat Messages:<\/strong><\/p>\n<p>The handleChatMessage function is an asynchronous function that handles the storage of chat messages into the GridDB container. Each message, along with its timestamp and the sender&#8217;s username, is inserted into the <code>Chat<\/code> container. This function illustrates how new data is added to GridDB in real-time.<\/p>\n<p>Let\u2019s create an <code>index.html<\/code> file by clicking <code>New File.<\/code><\/p>\n<p><strong>Add the following code to it:<\/strong><\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Chat App&lt;\/title&gt;\n    &lt;meta\n      name=\"viewport\"\n      content=\"width=device-width,minimum-scale=1,initial-scale=1\"\n    \/&gt;\n    &lt;style&gt;\n      body {\n        margin: 0;\n        font-family: sans-serif;\n      }\n\n      form {\n        position: fixed;\n        bottom: 0;\n        left: 0;\n        width: 100%;\n        display: flex;\n        box-sizing: border-box;\n        padding: 0.25rem;\n      }\n\n      form input {\n        border: 0;\n        padding: 0.5rem;\n        width: 100%;\n        outline: 0;\n        margin-right: 0.5rem;\n        border-radius: 0.25rem;\n        background: #ccc;\n      }\n\n      form button {\n        width: 6rem;\n        background-color: #1b8c00;\n        color: white;\n        border: none;\n        padding: 0.5rem;\n        cursor: pointer;\n        border-radius: 0.25rem;\n        text-transform: uppercase;\n      }\n\n      form button:hover {\n        background-color: #166d01;\n      }\n\n      .messages {\n        margin: 0;\n        padding: 0;\n        margin-bottom: 3rem;\n      }\n\n      .messages li {\n        padding: 0.5rem;\n      }\n\n      .messages li:nth-child(odd) {\n        background: #eee;\n      }\n    &lt;\/style&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;ul class=\"messages\"&gt;&lt;\/ul&gt;\n    &lt;form&gt;\n      &lt;input type=\"text\" class=\"input\" autocomplete=\"off\" autofocus \/&gt;\n      &lt;button&gt;Send&lt;\/button&gt;\n    &lt;\/form&gt;\n    &lt;!--Client side--&gt;\n    &lt;script src=\"\/socket.io\/socket.io.js\"&gt;&lt;\/script&gt;\n\n    &lt;script&gt;\n      const form = document.querySelector(\"form\");\n      const input = document.querySelector(\".input\");\n      const messages = document.querySelector(\".messages\");\n      const username = prompt(\"Please Enter Your Name\");\n      const socket = io();\n\n      form.addEventListener(\n        \"submit\",\n        function (event) {\n          event.preventDefault();\n\n          \/\/ addMessage(username + \": \" + input.value);\n\n          socket.emit(\"chat_message\", {\n            username: username,\n            message: input.value,\n          });\n\n          input.value = \"\";\n          return false;\n        },\n        false\n      );\n\n      socket.on(\"chat_message\", function (data) {\n        addMessage(data.username + \": \" + data.message);\n      });\n\n      socket.on(\"user_join\", function (data) {\n        addMessage(data + \" Has Joined\");\n        fetch(\"\/api\/messages\", {\n          method: \"GET\",\n          headers: {\n            \"Content-Type\": \"application\/json\",\n          },\n        })\n          .then((res) =&gt; {\n            res\n              .json()\n              .then((data) =&gt; {\n                console.log(data);\n                data.forEach((message) =&gt; {\n                  addMessage(message.username + \": \" + message.message);\n                });\n              })\n              .catch((err) =&gt; {\n                console.log(err);\n              });\n          })\n          .catch((err) =&gt; {\n            console.log(err);\n          });\n      });\n\n      socket.on(\"user_leave\", function (data) {\n        addMessage(data + \" Has Left\");\n      });\n      socket.on(\"disconnect\", function () {\n        addMessage(\"You Have Disconnected\");\n      });\n\n      addMessage(\"You Have Joined As \" + username);\n      socket.emit(\"user_join\", username);\n\n      function addMessage(message) {\n        const li = document.createElement(\"li\");\n        li.innerHTML = message;\n        messages.appendChild(li);\n        window.scrollTo(0, document.body.scrollHeight);\n      }\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<\/div>\n<p><strong>Explanation of the code:<\/strong><\/p>\n<p>The above code represents the client-side part of a real-time chat program. Using a &lt;style&gt; section to incorporate styling, the structure is conventional HTML5. An unordered list of chat messages is included in the body, along with a fixed form with an input field and a &#8216;Send&#8217; button at the bottom for message entry. Real-time communication is made possible by the embedded JavaScript connecting to the server using the Socket.io framework. Login requires user input, and some activities, such as submitting a form, cause the server to receive Socket.io events. Additionally, the script updates the user interface dynamically by listening to different server-sent events.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/ld_lib.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/ld_lib.png\" alt=\"\" width=\"1368\" height=\"768\" class=\"aligncenter size-full wp-image-30000\" srcset=\"\/wp-content\/uploads\/2024\/02\/ld_lib.png 1368w, \/wp-content\/uploads\/2024\/02\/ld_lib-300x168.png 300w, \/wp-content\/uploads\/2024\/02\/ld_lib-1024x575.png 1024w, \/wp-content\/uploads\/2024\/02\/ld_lib-768x431.png 768w, \/wp-content\/uploads\/2024\/02\/ld_lib-150x85.png 150w, \/wp-content\/uploads\/2024\/02\/ld_lib-600x337.png 600w\" sizes=\"(max-width: 1368px) 100vw, 1368px\" \/><\/a><\/p>\n<p>Then, run the application by running the following command:<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-sh\">node server.js<\/code><\/pre>\n<\/div>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/start.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/start.png\" alt=\"\" width=\"887\" height=\"318\" class=\"aligncenter size-full wp-image-30002\" srcset=\"\/wp-content\/uploads\/2024\/02\/start.png 887w, \/wp-content\/uploads\/2024\/02\/start-300x108.png 300w, \/wp-content\/uploads\/2024\/02\/start-768x275.png 768w, \/wp-content\/uploads\/2024\/02\/start-600x215.png 600w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/a><\/p>\n<p>Now, open the browser and type the URL <code>http:\/\/localhost:3000\/<\/code> to launch the application as <code>user 1<\/code>. Then, open another browser window and type the same URL to launch the application as <code>user 2<\/code>.<\/p>\n<p>Now, type a message in the input field and click the <code>Send<\/code> button. The message will be displayed in both the browser windows.<\/p>\n<p>The output will look like this:<\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/chat_ui.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/02\/chat_ui.png\" alt=\"\" width=\"1368\" height=\"768\" class=\"aligncenter size-full wp-image-30004\" srcset=\"\/wp-content\/uploads\/2024\/02\/chat_ui.png 1368w, \/wp-content\/uploads\/2024\/02\/chat_ui-300x168.png 300w, \/wp-content\/uploads\/2024\/02\/chat_ui-1024x575.png 1024w, \/wp-content\/uploads\/2024\/02\/chat_ui-768x431.png 768w, \/wp-content\/uploads\/2024\/02\/chat_ui-150x85.png 150w, \/wp-content\/uploads\/2024\/02\/chat_ui-600x337.png 600w\" sizes=\"(max-width: 1368px) 100vw, 1368px\" \/><\/a><\/p>\n<p>As you can see, the message is displayed in both the browser windows. The message is also stored in GridDB.<\/p>\n<h2>Conclusion:<\/h2>\n<p>In this guide, we have walked through the steps to create a real-time chat application, combining the power of Node.js with the robust data management capabilities of GridDB. This combination of technologies, along with Express for streamlined server setup and Socket.io for seamless real-time communication, forms a dynamic platform for immediate, interactive messaging.<\/p>\n<p>You&#8217;ve seen how each component plays a crucial role: Node.js serves as the backbone, GridDB manages the chat data efficiently, Express simplifies the web server&#8217;s configuration, and Socket.io ensures that messages are exchanged in real time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: In this tutorial, we&#8217;re going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with friends, family, or colleagues from anywhere in the world. That&#8217;s the power of real-time chat, and today, you&#8217;re going to learn how to build one from scratch! We&#8217;ll be using Node.js, a [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":27878,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[121],"tags":[],"class_list":["post-46792","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to build a Real-time Chat Application with Node.js and GridDB | GridDB: Open Source Time Series Database for IoT<\/title>\n<meta name=\"description\" content=\"Introduction: In this tutorial, we&#039;re going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to build a Real-time Chat Application with Node.js and GridDB | GridDB: Open Source Time Series Database for IoT\" \/>\n<meta property=\"og:description\" content=\"Introduction: In this tutorial, we&#039;re going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with\" \/>\n<meta property=\"og:url\" content=\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/\" \/>\n<meta property=\"og:site_name\" content=\"GridDB: Open Source Time Series Database for IoT\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/griddbcommunity\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-28T08:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-13T20:56:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/griddb.net\/wp-content\/uploads\/2021\/11\/img1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"543\" \/>\n\t<meta property=\"og:image:height\" content=\"273\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"griddb-admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@GridDBCommunity\" \/>\n<meta name=\"twitter:site\" content=\"@GridDBCommunity\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"griddb-admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/\"},\"author\":{\"name\":\"griddb-admin\",\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233\"},\"headline\":\"How to build a Real-time Chat Application with Node.js and GridDB\",\"datePublished\":\"2024-02-28T08:00:00+00:00\",\"dateModified\":\"2025-11-13T20:56:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/\"},\"wordCount\":1218,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage\"},\"thumbnailUrl\":\"\/wp-content\/uploads\/2021\/11\/img1.png\",\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/\",\"url\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/\",\"name\":\"How to build a Real-time Chat Application with Node.js and GridDB | GridDB: Open Source Time Series Database for IoT\",\"isPartOf\":{\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage\"},\"thumbnailUrl\":\"\/wp-content\/uploads\/2021\/11\/img1.png\",\"datePublished\":\"2024-02-28T08:00:00+00:00\",\"dateModified\":\"2025-11-13T20:56:53+00:00\",\"description\":\"Introduction: In this tutorial, we're going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage\",\"url\":\"\/wp-content\/uploads\/2021\/11\/img1.png\",\"contentUrl\":\"\/wp-content\/uploads\/2021\/11\/img1.png\",\"width\":543,\"height\":273},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#website\",\"url\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/\",\"name\":\"GridDB: Open Source Time Series Database for IoT\",\"description\":\"GridDB is an open source time-series database with the performance of NoSQL and convenience of SQL\",\"publisher\":{\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#organization\",\"name\":\"Fixstars\",\"url\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png\",\"contentUrl\":\"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png\",\"width\":200,\"height\":83,\"caption\":\"Fixstars\"},\"image\":{\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/griddbcommunity\/\",\"https:\/\/x.com\/GridDBCommunity\",\"https:\/\/www.linkedin.com\/company\/griddb-by-toshiba\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233\",\"name\":\"griddb-admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g\",\"caption\":\"griddb-admin\"},\"url\":\"https:\/\/griddb.net\/en\/author\/griddb-admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to build a Real-time Chat Application with Node.js and GridDB | GridDB: Open Source Time Series Database for IoT","description":"Introduction: In this tutorial, we're going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/","og_locale":"en_US","og_type":"article","og_title":"How to build a Real-time Chat Application with Node.js and GridDB | GridDB: Open Source Time Series Database for IoT","og_description":"Introduction: In this tutorial, we're going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with","og_url":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/","og_site_name":"GridDB: Open Source Time Series Database for IoT","article_publisher":"https:\/\/www.facebook.com\/griddbcommunity\/","article_published_time":"2024-02-28T08:00:00+00:00","article_modified_time":"2025-11-13T20:56:53+00:00","og_image":[{"width":543,"height":273,"url":"https:\/\/griddb.net\/wp-content\/uploads\/2021\/11\/img1.png","type":"image\/png"}],"author":"griddb-admin","twitter_card":"summary_large_image","twitter_creator":"@GridDBCommunity","twitter_site":"@GridDBCommunity","twitter_misc":{"Written by":"griddb-admin","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#article","isPartOf":{"@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/"},"author":{"name":"griddb-admin","@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233"},"headline":"How to build a Real-time Chat Application with Node.js and GridDB","datePublished":"2024-02-28T08:00:00+00:00","dateModified":"2025-11-13T20:56:53+00:00","mainEntityOfPage":{"@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/"},"wordCount":1218,"commentCount":1,"publisher":{"@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#organization"},"image":{"@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage"},"thumbnailUrl":"\/wp-content\/uploads\/2021\/11\/img1.png","articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/","url":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/","name":"How to build a Real-time Chat Application with Node.js and GridDB | GridDB: Open Source Time Series Database for IoT","isPartOf":{"@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage"},"image":{"@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage"},"thumbnailUrl":"\/wp-content\/uploads\/2021\/11\/img1.png","datePublished":"2024-02-28T08:00:00+00:00","dateModified":"2025-11-13T20:56:53+00:00","description":"Introduction: In this tutorial, we're going to dive into the exciting world of real-time chat applications. Imagine being able to instantly connect with","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/griddb.net\/en\/blog\/how-to-build-a-real-time-chat-application-with-node-js-and-griddb\/#primaryimage","url":"\/wp-content\/uploads\/2021\/11\/img1.png","contentUrl":"\/wp-content\/uploads\/2021\/11\/img1.png","width":543,"height":273},{"@type":"WebSite","@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#website","url":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/","name":"GridDB: Open Source Time Series Database for IoT","description":"GridDB is an open source time-series database with the performance of NoSQL and convenience of SQL","publisher":{"@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#organization","name":"Fixstars","url":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/logo\/image\/","url":"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png","contentUrl":"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png","width":200,"height":83,"caption":"Fixstars"},"image":{"@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/griddbcommunity\/","https:\/\/x.com\/GridDBCommunity","https:\/\/www.linkedin.com\/company\/griddb-by-toshiba"]},{"@type":"Person","@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233","name":"griddb-admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g","caption":"griddb-admin"},"url":"https:\/\/griddb.net\/en\/author\/griddb-admin\/"}]}},"_links":{"self":[{"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/posts\/46792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/comments?post=46792"}],"version-history":[{"count":1,"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/posts\/46792\/revisions"}],"predecessor-version":[{"id":51454,"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/posts\/46792\/revisions\/51454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/media\/27878"}],"wp:attachment":[{"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/media?parent=46792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/categories?post=46792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/griddb.net\/en\/wp-json\/wp\/v2\/tags?post=46792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}