Created
November 2, 2021 13:39
-
-
Save jenschr/f929d1685aaeb7a5bcf4e07fb7eb4da4 to your computer and use it in GitHub Desktop.
Chat example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const http = require('http'); | |
const server = http.createServer(app); | |
const { Server } = require("socket.io"); | |
const io = new Server(server); | |
app.get('/', (req, res) => { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
io.on('connection', (socket) => { | |
socket.on('chat message', (msg) => { | |
io.emit('chat message', msg); | |
}); | |
}); | |
io.on('connection', (socket) => { | |
console.log('a user connected'); | |
socket.on('disconnect', () => { | |
console.log('user disconnected'); | |
}); | |
}); | |
server.listen(3000, () => { | |
console.log('listening on *:3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment