Skip to content

A lightweight FastAPI + WebSocket based chat app for local networks. Supports a public chatroom and token-protected private spaces. Works on PC & mobile via browser.

License

Notifications You must be signed in to change notification settings

akashdip2001/LAN-Token-Chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

27 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ’ฌ LAN Token Chat

A lightweight LAN chat app built with FastAPI + WebSockets.
Supports both public chatrooms (everyone on the network) and token-protected private spaces for secure group conversations.

img 01 (1)

First update No CSS

Works directly in the browser โ€” no extra software needed!
Supports desktop & mobile displays.


โญ main project concept โญ

Token-Based-Authentication


โœจ Features

  • ๐ŸŒ Public Chat โ€” all devices on the LAN can join easily.
  • ๐Ÿ”‘ Private Chat (Token) โ€” create a personal space with a unique token.
    Only people who enter that token can join.
  • ๐Ÿ•’ Timestamps on every message.
  • ๐Ÿ“ฑ Responsive UI โ€” works on PC & mobile.
  • โšก Powered by FastAPI + WebSockets.

๐Ÿ” User Session Features

๐Ÿ” User Session Features

  1. First-Time Login

    • When a new user opens the app, they are prompted to enter:

      • A username
      • A password (set by the user, minimum 4 characters for easier recall)
    • The username and password are stored locally in the browser (localStorage) as part of a session object.

  2. Password Security (Masked View)

    • Whenever the password is shown in popups, only the 2nd and 3rd characters are hidden.

    • Examples:

      • 1234 โ†’ 1**4
      • 12345 โ†’ 1**45
      • abcdef โ†’ a**def
  3. 30-Minute Session Timeout

    • If the page is refreshed or reopened after 30 minutes of inactivity, a popup appears:

      • Option 1: Continue with the saved username (requires entering the saved password).
      • Option 2: Start a new session with a new username and password (clears old data).
  4. Username Bar Interaction

    • Clicking the username at the top of the page shows a popup with:

      • The current username

      • The current password (masked)

      • Two buttons:

        • โ€œUse another usernameโ€ โ†’ allows setting a new username & password.
        • โ€œCancelโ€ โ†’ closes the popup without changes.
  5. Reload & Cache Refresh

    • Choosing to reset (new session) clears all stored data, including username, password, and messages.
    • On reset, the page reloads, ensuring that any new CSS/JS updates are also applied.

โšก This makes the app behave more like a real chat system with basic account persistence, session expiry, and manual username management, while still being lightweight and browser-based.




IMG_20250817_221919211_HDR

Second Update


๐Ÿ“‚ Project Structure


LAN-Token-Chat/
โ”‚โ”€โ”€ main.py       # Backend server (FastAPI + WebSocket)
โ”‚โ”€โ”€ index.html    # Frontend UI (simple chat interface)
โ”‚โ”€โ”€ README.md     # Project documentation


๐Ÿš€ Setup & Run

1. Clone the repository

git clone https://github.com/akashdip2001/LAN-Token-Chat.git
cd LAN-Token-Chat

2. Install dependencies

pip install fastapi uvicorn

3. Run the server

python main.py

4. Open in browser

Screenshot (692)

On the host computer:

http://127.0.0.1:8000

On another device in the same LAN:

http://<your-local-ip>:8000

๐Ÿ› ๏ธ How It Works

ย  ย 

Public Chat

  • Everyone can join by pressing Join Public Chat.

Private Chat

  1. One person clicks Create Private Space. โ†’ The server generates a token (example: a3f9c1b2).
  2. Share this token with friends.
  3. Others click Join Private Space, enter the token, and join the private chatroom.

โœ… Only users with the correct token can access that private space.


[12:45:01] Alice: Hello everyone!
[12:45:15] Bob: Hi Akashdip ๐Ÿ‘‹

Private Chat (Token: 7fa21bc3)

[13:10:22] Alice: Secret group chat ๐Ÿ•ต๏ธ
[13:10:45] Bob: Only we can see this ๐Ÿ˜Ž

img 3 (2)


๐Ÿ—๏ธ Tech Stack

  • Backend: Python, FastAPI, WebSockets
  • Frontend: HTML, JavaScript, CSS
  • Environment: Runs on any OS with Python 3

๐Ÿ”ฎ Future Ideas

  • Encrypt messages for extra privacy.


๐Ÿšฉ Username Issue: preview-xxxx showing in Online Users

Problem

When testing with multiple devices (e.g., PC username 12345 and mobile username akashdip), an extra username such as preview-15f1 appears in the online users list.

Example seen in UI:

Request
12345
Request
akashdip
Request
preview-15f1

This extra user is not a real user, but comes from the landing page preview feature.


Cause

In script.js, the landing page (index.html) opens a dummy WebSocket to fetch live messages for the preview box:

(function previewSocket() {
  const anon = 'preview-' + Math.random().toString(16).slice(2, 6);
  const socket = new WebSocket(`ws://${location.host}/ws/public/${anon}`);
  state.previewSocket = socket;
})();
  • This generates a fake user (preview-xxxx) each time someone loads the landing page.
  • The server treats it as a real participant, so it shows up in the online users list.

Solution

Option 1 (Recommended): Hide preview usernames from the online list

Edit the renderOnlineUsers(users) function in script.js:

function renderOnlineUsers(users) {
  const wrap = document.getElementById('onlineUsers');
  if (!wrap) return;
  wrap.innerHTML = '';
  users
    .filter(u => !u.startsWith('preview-'))  // ๐Ÿ”ฅ filter out dummy preview clients
    .forEach(u => {
      const row = document.createElement('div'); row.className = 'user-pill';
      const name = document.createElement('div'); name.textContent = u;
      const btn = document.createElement('button'); btn.className = 'glass-btn small'; btn.textContent = 'Request';
      btn.setAttribute('data-user', u);
      row.appendChild(name); row.appendChild(btn);
      wrap.appendChild(row);
    });
}

Now, only real usernames (e.g., 12345, akashdip) appear.


Option 2: Disable preview feature

If the preview box on index.html is not needed, simply comment out the preview socket code:

// (function previewSocket() {
//   const anon = 'preview-' + Math.random().toString(16).slice(2, 6);
//   const socket = new WebSocket(`ws://${location.host}/ws/public/${anon}`);
//   state.previewSocket = socket;
// })();

This will stop generating preview-* users but removes live preview on the landing page.


Notes

  • This is not a bug with username sessions, but a side-effect of the preview socket.
  • Fixing it requires either filtering out fake preview usernames (preferred) or removing the preview feature.

About

A lightweight FastAPI + WebSocket based chat app for local networks. Supports a public chatroom and token-protected private spaces. Works on PC & mobile via browser.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published