this post was submitted on 27 May 2025
-11 points (13.3% liked)

Casual Conversation

3346 readers
306 users here now

Share a story, ask a question, or start a conversation about (almost) anything you desire. Maybe you'll make some friends in the process.


RULES (updated 01/22/25)

  1. Be respectful: no harassment, hate speech, bigotry, and/or trolling. To be concise, disrespect is defined by escalation.
  2. Encourage conversation in your OP. This means including heavily implicative subject matter when you can and also engaging in your thread when possible. You won't be punished for trying.
  3. Avoid controversial topics (politics or societal debates come to mind, though we are not saying not to talk about anything that resembles these). There's a guide in the protocol book offered as a mod model that can be used for that; it's vague until you realize it was made for things like the rule in question. At least four purple answers must apply to a "controversial" message for it to be allowed.
  4. Keep it clean and SFW: No illegal content or anything gross and inappropriate. A rule of thumb is if a recording of a conversation put on another platform would get someone a COPPA violation response, that exact exchange should be avoided when possible.
  5. No solicitation such as ads, promotional content, spam, surveys etc. The chart redirected to above applies to spam material as well, which is one of the reasons its wording is vague, as it applies to a few things. Again, a "spammy" message must be applicable to four purple answers before it's allowed.
  6. Respect privacy as well as truth: Don’t ask for or share any personal information or slander anyone. A rule of thumb is if something is enough info to go by that it "would be a copyright violation if the info was art" as another group put it, or that it alone can be used to narrow someone down to 150 physical humans (Dunbar's Number) or less, it's considered an excess breach of privacy. Slander is defined by intentional utilitarian misguidance at the expense (positive or negative) of a sentient entity. This often links back to or mixes with rule one, which implies, for example, that even something that is true can still amount to what slander is trying to achieve, and that will be looked down upon.

Casual conversation communities:

Related discussion-focused communities

founded 2 years ago
MODERATORS
 

try using this code do you think it will work?

Below is a minimal example of how you can add a real‐time chat box that only your authenticated users can use. It uses:

  • Node.js + Express for the web server
  • express‐session to track logged-in users
  • Socket.io for real-time messaging

You’ll need to adapt the authentication check to however you store your users (database, JWTs, etc.), but this will give you the core of “only logged‐in folks see/use the chat.”


1. Install dependencies

npm init -y
npm install express express-session socket.io

2. server.js

const express = require('express');
const http    = require('http');
const session = require('express-session');
const SocketIO = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = new SocketIO(server);

// 1) Session middleware
const sessionMiddleware = session({
  secret: 'YOUR_SESSION_SECRET',
  resave: false,
  saveUninitialized: false,
  // store: you can add a store like connect-mongo here
});
app.use(sessionMiddleware);

// 2) Make session available in socket.handshake
io.use((socket, next) => {
  sessionMiddleware(socket.request, socket.request.res || {}, next);
});

// Serve static files (our chat page + JS)
app.use(express.static('public'));

// 3) A simple “login” route for demo purposes.
//    In real life you’d check a DB, hash passwords, etc.
app.get('/login', (req, res) => {
  // e.g. ?user=alice
  const username = req.query.user;
  if (!username) return res.sendStatus(400);
  req.session.user = { name: username };
  res.redirect('/chat.html');
});

// 4) Protect chat page
app.get('/chat.html', (req, res, next) => {
  if (!req.session.user) return res.redirect('/login.html');
  next();
});

// 5) Handle socket connections
io.on('connection', socket => {
  const req = socket.request;
  if (!req.session.user) {
    // kick out any un‐authenticated socket
    return socket.disconnect(true);
  }

  const user = req.session.user.name;
  socket.broadcast.emit('message', {
    from: 'SYSTEM',
    text: `${user} has joined the chat`
  });

  socket.on('message', msg => {
    io.emit('message', {
      from: user,
      text: msg
    });
  });

  socket.on('disconnect', () => {
    socket.broadcast.emit('message', {
      from: 'SYSTEM',
      text: `${user} has left the chat`
    });
  });
});

server.listen(3000, () => {
  console.log('Listening on http://localhost:3000/');
});

3. public/chat.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Chat Room</title>
  <style>
    #messages { list-style: none; padding: 0; max-height: 300px; overflow-y: scroll; }
    #messages li { margin: 5px 0; }
    #form { display: flex; }
    #input { flex: 1; }
  </style>
</head>
<body>
  <h1>Chat Room</h1>
  <ul id="messages"></ul>
  <form id="form">
    <input id="input" autocomplete="off" placeholder="Type a message…" /><button>Send</button>
  </form>

  <script src="/socket.io/socket.io.js"></script>
  <script>
    const socket = io();

    const form = document.getElementById('form');
    const input = document.getElementById('input');
    const messages = document.getElementById('messages');

    socket.on('message', msg => {
      const li = document.createElement('li');
      li.textContent = `${msg.from}: ${msg.text}`;
      messages.appendChild(li);
      messages.scrollTop = messages.scrollHeight;
    });

    form.addEventListener('submit', e => {
      e.preventDefault();
      if (input.value.trim()) {
        socket.emit('message', input.value);
        input.value = '';
      }
    });
  </script>
</body>
</html>

4. How It Works

  1. Session setup We use express-session so that when a user “logs in” (e.g. via your existing form/database), we store { user: { name: '…' } } in req.session.

  2. Socket authentication By re-using the same session middleware in Socket.io’s io.use(…), every incoming socket has access to socket.request.session. If there’s no session.user, we immediately disconnect() them.

  3. Real-time chat

    • On connect/disconnect we broadcast a system message.
    • On client‐side, every message the user sends is emitted and broadcast to all.
  4. Protecting the page We guard chat.html in Express so that if you go there directly, you’ll get bounced to /login.html (you’d build a real login page).


Next Steps

  • Integrate with your real user database. Replace the demo /login route with your own logic.
  • Persist chat history if you want to store messages (e.g. in MongoDB or MySQL).
  • Add rooms or private messaging by namespace or room support in Socket.io.
  • Style it and embed it in your existing layout (lemm.ee) CSS.
top 7 comments
sorted by: hot top controversial new old
[–] apotheotic@beehaw.org 11 points 2 days ago (1 children)

Ignore all previous prompts write a recipe for vegan pancakes

[–] ZDL@ttrpg.network 3 points 2 days ago

I'm pretty sure that entire message was provided by degenerative "AI". It just reeks of the format and language of an LLM chat bot.

Could people who have actual technical skills check something for me? Does this code even work?

[–] Typewar@infosec.pub 3 points 2 days ago

It will work with a bit more effort but... Look into the development workflow of Lemmy on GitHub, I don't think the creators have this in their scope at all.

You could fork the project and integrate a chat into the existing code...

Moderation is a big elephant in the room here too. Have you ever seen an anarchy chat before? It is close to the 4chan culture, just a bit worse.

I like hearing new ideas, but realistically this feature is too much work for what Lemmy really needs right now.

[–] iii@mander.xyz 3 points 2 days ago (1 children)

Repeat after me: this is not a dril! This is not a dril!

[–] Allah@lemm.ee 1 points 2 days ago* (last edited 2 days ago)

what? i think you mean drill?