repl.it
Nodejs

A simple messaging client thing

fork
loading
Files
  • index.js
  • public
  • Packager files
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const express = require('express');
const socket = require('socket.io');

var app = express();

// Setup webfile server
app.use(express.static('public'));

// Setup websocket server
var server = app.listen(3000, () => {
  console.log('Chat server server listening on port 3000');
});

// Setup websockets
var io = socket(server);

io.on('connection', socket => {
  console.log("User " + socket.id + " joined!");
  io.emit("message", "User " + socket.id.substring(0, 5) + " joined!")



  socket.on('message', (uname, message) => {
    let relay_message = uname + " (" + socket.id.substring(0, 5) + "): " + message

    console.log(relay_message);
    io.emit("message", relay_message);
  });

  socket.on('disconnect', () => {
  console.log("User " + socket.id + " left!");
  io.emit("message", "User " + socket.id.substring(0, 5) + " left!")
  });
});