Last active
August 20, 2022 23:27
-
-
Save Theodore-Kelechukwu-Onyejiaku/25c5d86cbc99062221662423d8115919 to your computer and use it in GitHub Desktop.
Server file for Open Group Chat Application
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 app = require('express')(); | |
const server = require('http').createServer(app); | |
const cors = require('cors'); | |
// configure socket.io to allow cross-origin requests | |
const io = require('socket.io')(server, { cors: { origin: '*' } }); | |
// set up our PORT | |
const MY_PREFERRED_PORT = 8000; | |
const PORT = process.env.PORT || MY_PREFERRED_PORT; | |
// allow cors | |
app.use(cors()); | |
// on socket connection | |
io.on('connection', (socket) => { | |
// get active or connected socket | |
socket.emit('activeUsers'); | |
// get socket id of connected socket | |
socket.emit('getId', socket.id); | |
// when a chat event is emitted | |
socket.on('chat', (id, chat) => { | |
io.emit('sendChat', id, chat, socket.id); | |
}); | |
// when a user changes their username | |
socket.on('usernameChange', (username, socketid) => { | |
socket.broadcast.emit('resetChat', username, socketid); | |
}); | |
// when a user is Typing | |
socket.on('userTyping', (socketid, type) => { | |
socket.broadcast.emit('someoneTyping', socketid, type); | |
}); | |
// get all connected sockets | |
socket.on('activeUsers', () => { | |
// gets all connected sockets | |
const onlineUsers = io.engine.clientsCount; | |
io.emit('countUsers', onlineUsers); | |
}); | |
// when a socket gets disconnected | |
socket.on('disconnect', () => { | |
socket.broadcast.emit('disconnectNotification', socket.id); | |
const onlineUsers = io.engine.clientsCount; | |
socket.broadcast.emit('countUsers', onlineUsers); | |
}); | |
}); | |
server.listen(PORT, () => { | |
console.log(`Application running successfully on port: ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment