Skip to content

Instantly share code, notes, and snippets.

@dkhd
Created Feb 18, 2019
Embed
What would you like to do?
Build A Group-Chat App in 30 Lines Using Node.js
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.get('/', function(req, res) {
res.render('index.ejs');
});
io.sockets.on('connection', function(socket) {
socket.on('username', function(username) {
socket.username = username;
io.emit('is_online', '🔵 <i>' + socket.username + ' join the chat..</i>');
});
socket.on('disconnect', function(username) {
io.emit('is_online', '🔴 <i>' + socket.username + ' left the chat..</i>');
})
socket.on('chat_message', function(message) {
io.emit('chat_message', '<strong>' + socket.username + '</strong>: ' + message);
});
});
const server = http.listen(8080, function() {
console.log('listening on *:8080');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment