Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
server logic started
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricouellet committed Jan 23, 2022
1 parent 87115d2 commit d5b978b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# NPM modules
/node_modules
/node_modules

# NPM cache
package-lock.json
47 changes: 47 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('path');
const helmet = require('helmet');
const http = require('http');
const socketIoServer = require('socket.io');
const express = require('express');

const PORT = parseInt(process.env.PORT) || 3000;

const EV_CONNECT = 'connection';
const EV_MESSAGE = 'message';
const EV_CHAT_MESSAGE = 'chat_message';
const EV_DISCONNECT = 'disconnect';

const app = express();
const server = http.createServer(app);
const io = socketIoServer(server);

// Express middlewares
app.use(helmet());
app.use(express.static(path.join(__dirname, 'public')));

// Listen for socket connections
io.on(EV_CONNECT, (socket) => {
console.log(`client socket ${socket.id} connected`);

// Welcome the new user
socket.emit(EV_MESSAGE, 'Welcome to NoChat!');

// Send to all users except the new user
socket.broadcast.emit(EV_MESSAGE, 'A user has joined the chat.');

// When a user disconnects
socket.on(EV_DISCONNECT, () => {
io.emit(EV_MESSAGE, 'A user has left the chat.');
});

// When a user sends a message
socket.on(EV_CHAT_MESSAGE, (message) => {
// We broadcast it to every user
io.emit(EV_MESSAGE, message);
});
});

// Start Express server
server.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});

0 comments on commit d5b978b

Please sign in to comment.