Home / Media and Real Time / Live Chat App

Live Chat App in JavaScript, Free with Live Demo

Free real time chat in plain JavaScript. Rooms, nicknames, typing indicators and reactions, working between tabs with no server or across devices with a WebSocket server.

Open live demoDownload HTML fileView code on GitHub
Live Chat App JavaScript project: a real time chat with rooms and typing indicators

Runs on: WebSocket. Every modern browser. For devices on different networks, host server.js somewhere with wss:// (HTTPS pages need secure WebSockets).

What is the Live Chat App?

Open two tabs and chat between them right away. Run the tiny included server and people on other phones and laptops can join the same rooms, see who is typing and react to messages.

Messages are small JSON events. Without a server they travel over BroadcastChannel. With one, they go over a WebSocket, and the 20 line server.js simply passes each event to everyone else.

Good for

  • Support chat widgets
  • Team and community rooms
  • Learning WebSocket
  • Real time features in any app

What this project does

How it works

  1. Pick a transportWith no server address, messages go over BroadcastChannel between tabs. With one, they go over a WebSocket.
  2. Send eventsMessages, typing, joins and reactions are small JSON events with a room name and a sender id.
  3. RenderEach tab applies events to its local state and redraws the room. The server only relays events, it keeps nothing.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

const ws = new WebSocket("wss://chat.example.com");
ws.onmessage = (e) => handle(JSON.parse(e.data));

function send(type, data) {
  ws.send(JSON.stringify({ type, room, from: me.id, name: me.name, ...data }));
}
input.addEventListener("input", throttle(() => send("typing"), 1500));
form.addEventListener("submit", () => send("message", { text: input.value, id: crypto.randomUUID() }));
// server.js relays every message to everyone else
wss.on("connection", (s) => s.on("message", (d) => wss.clients.forEach((c) => c !== s && c.send(d.toString()))));

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

How do I run the chat server?

Install Node, run npm install ws, then node server.js. Enter ws://your-ip:8788 in the page on each device.

Are messages saved?

Each browser keeps the last 200 messages per room. The server keeps nothing, it only relays.

Can I host it online?

Yes. Deploy server.js to any Node host and use a wss:// address, since HTTPS pages need secure WebSockets.

More Media and Real Time projects