Skip to content

About

A node semi-persistent chat (messages are deleted after X hours)

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Repository files navigation

Semi Persistent Chat

That project is a simple semi-persistent PWA chat using web socket. Messages are kept in memory and purged after X hours on the server (configurable).

URLs sent in chat will be transformed as links. For media (image, audio or video), a preview will be displayed.

Login scren:

Screenshot of login screen

Chat Screen :

Screenshot of a chat

Features

  • multi-room chat
  • show media preview (image, video, audio)
  • display web page title
  • works on mobile
  • edit or delete sent messages
  • can reply to messages (1 reply level)
  • comment with emojis
  • messages are strikethrough when they reach their expiration time
  • optional client-side encryption (password based): the server only stores encrypted content

Encryption

On the home page, an optional password enables end-to-end encryption in the browser (AES-GCM 256, key derived with PBKDF2). When set:

  • messages (including pasted images) are encrypted before being sent: the server and Redis only ever see ciphertext. Messages that could not be decrypted (wrong or missing password) are displayed as 🔒 and their actions are disabled
  • the password lives in the URL fragment (#pwd=...), after the room name. Fragments are never sent to the server: sharing the full URL with friends shares the password with them, sharing only the room name keeps messages unreadable for them
  • the URL (and so the password) stays in the browser history: consider that before sharing your screen or history
  • encryption requires a secure context (HTTPS or localhost): the password field is disabled otherwise

Limitations to be aware of:

  • room names, usernames, timestamps, emojis reactions and message sizes are not encrypted (they are needed to display the room)
  • losing the password (or the URL fragment) means losing the ability to read the room history

Configuration

Copy .env.dist and update it according your needs. You should at least change REDIS_PASSWORD and SECRET (used for websocket authentification) and ORIGIN (your domain).

Other settings are :

  • REDIS_HOST
  • REDIS_PORT
  • MSG_RETENTION_HOURS: Number of hours before messages expires. This setting controls both server-side message cleanup and client-side strikethrough behavior
  • MAX_MSG_SIZE: max messsage size in bytes
  • URL_CACHE : max number of urls titles in cache

Redis

The application uses Redis for message storage with automatic expiration. Messages are stored in sorted sets with timestamps as scores, allowing efficient range queries and automatic cleanup.

Key features of Redis usage:

  • Messages are stored with TTL (Time-To-Live) based on MSG_RETENTION_HOURS setting
  • Hourly cleanup job removes expired messages automatically
  • Efficient sorted set operations for message retrieval and expiration

Start redis

docker-compose up -d

Redis inspection

docker-compose exec redis redis-cli
auth "redis-password"

To dev

run npm run dev:client and npm run dev:server. Go to http://localhost:6060 (or what you set in PORT) Server will inject env vars and vite middleware (for hot reload) in dev.

To deploy on production using only node

Run npm run build to generate static files into build directory, then run npm run start which will serve static files.

systemd deployment

When running the server under systemd:

  • start node directly (ExecStart=... node dist/server/index.js), not npm run start
  • order the unit after the Redis provider (After=redis.service, or After=docker.service if Redis runs in a container) so this service stops before Redis on shutdown
  • set a bounded TimeoutStopSec (e.g. 10s)

On SIGTERM / SIGINT / SIGHUP the server closes socket.io and HTTP connections, force-closes the Redis client and exits within ~5s.

nginx configuration

location /persistent-chat/ {
        proxy_pass http://127.0.0.1:6060/api/;
        proxy_http_version 1.1;
}
location /persistent-chat-ws/ {
        proxy_pass http://127.0.0.1:6060;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
}

License

MIT

About

A node semi-persistent chat (messages are deleted after X hours)

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages