Simple Chat app

Simple Chat app

If you are looking to build a simple chat app, there are several ways to go about it. One of the easiest ways is to use a combination of HTML, CSS, and JavaScript to create a front-end interface that interacts with a back-end server using a REST API.

Step 1: Setting up the front-end

The first step is to create the front-end interface using HTML, CSS, and JavaScript. This can be done using any code editor such as Visual Studio Code or Sublime Text. Here is a sample HTML code for a simple chat app:


<div id="chat-container">
  <div id="chat-messages">
    <ul id="messages">
    </ul>
  </div>
  <form id="chat-form">
    <input type="text" id="message-input" placeholder="Type message here...">
    <button type="submit" id="send-message">Send</button>
  </form>
</div>

The above code creates a simple chat interface with a container div, a message list, and a form to input new messages. We will use JavaScript to add functionality to this interface.

Step 2: Creating the back-end server

The next step is to create a back-end server that will handle the chat messages. This can be done using any programming language such as Node.js, Python, or PHP. Here is a sample Node.js code for the back-end:


const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

io.on("connection", (socket) => {
  console.log("a user connected");
  
  socket.on("disconnect", () => {
    console.log("user disconnected");
  });
  
  socket.on("chat message", (msg) => {
    console.log("message: " + msg);
    io.emit("chat message", msg);
  });
});

http.listen(3000, () => {
  console.log("listening on *:3000");
});

The above code creates a Node.js server that listens on port 3000 and uses Socket.io to handle real-time communication between clients. The server listens for "connection" events and logs when a user connects or disconnects. It also listens for "chat message" events and emits them to all connected clients.

Step 3: Adding functionality to the front-end

The final step is to add functionality to the front-end interface using JavaScript. We will use Socket.io to handle real-time communication with the back-end server. Here is a sample JavaScript code for the front-end:


const socket = io();

const messageList = document.getElementById("messages");

const chatForm = document.getElementById("chat-form");
const messageInput = document.getElementById("message-input");

chatForm.addEventListener("submit", (event) => {
  event.preventDefault();
  
  const message = messageInput.value;
  
  socket.emit("chat message", message);
  
  messageInput.value = "";
});

socket.on("chat message", (msg) => {
  const li = document.createElement("li");
  li.innerText = msg;
  messageList.appendChild(li);
});

The above code uses Socket.io to connect to the back-end server and listen for "chat message" events. When a user submits a message through the form, the code emits a "chat message" event with the message as the payload. When the server emits a "chat message" event, the code creates a new list item and appends it to the message list.

There are several other ways to build a simple chat app using different technologies such as Firebase, Pusher, or PubNub. However, the above approach using HTML, CSS, JavaScript, Node.js, and Socket.io is one of the simplest and most flexible ways to build a custom chat app.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe