Skip to content
the poor coder

Simple Chat app

Poor Man
Published
Reading time
3 min read
On this page

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:

Text

<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:

Text

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:

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC