Skip to content

Commit

Permalink
added port
Browse files Browse the repository at this point in the history
  • Loading branch information
frontdevguy committed Apr 24, 2020
1 parent 53f1cf7 commit fad91c4
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,33 @@ const express = require("express");
const socketio = require("socket.io");
const router = require("./server/route");
const app = express();
const { addUsers, removeUser, getAllUsers, getUser } = require("./server/user");
const {
addUsers,
removeUser,
getAllUsers,
getUser,
} = require("./server/user");

app.use(router);

const server = http.createServer(app);
const io = socketio(server);

io.on("connection", socket => {
io.on("connection", (socket) => {
socket.on("join", ({ name, room }, callback) => {
const { error, user } = addUsers({ id: socket.id, name, room });
if (error) return callback(error);

// message to only the newly joined user
socket.emit("message", {
user: "admin",
message: ` ${user.name}, welcome to to ${user.room} room`
message: ` ${user.name}, welcome to to ${user.room} room`,
});

// Message to the room except to the newly joined user
socket.broadcast.to(user.room).emit("message", {
user: "admin",
message: `${user.name} has just join the ${user.room} room`
message: `${user.name} has just join the ${user.room} room`,
});
socket.join(user.room);
callback();
Expand All @@ -43,13 +48,13 @@ io.on("connection", socket => {
if (user) {
io.to(user.room).emit("message", {
user: "admin",
message: `${user.name} has left the ${user.room} room`
message: `${user.name} has left the ${user.room} room`,
});
}
});
});

const PORT = 3001;
server.listen(PORT, function() {
const PORT = process.env.PORT || 3001;
server.listen(PORT, function () {
console.log(`The server has started on Port ${PORT}`);
});

0 comments on commit fad91c4

Please sign in to comment.