├── .gitignore ├── README.md ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | uploads -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NodeJSChatServer 2 | #### Socket io chat server made with node js 3 | 4 | ## Installation 5 | 6 | * clone the projet from the git repository 7 | * under your project directory execute this command 8 | 9 | ``` 10 | $ npm install 11 | ``` 12 | *this will install all the required dependencies 13 | 14 | * run your project using the following command 15 | 16 | ``` 17 | $ node server.js 18 | ``` 19 | 20 | * your project will run on htttp://localhost:3000/ 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.15.3", 13 | "nodemon": "^1.11.0", 14 | "socket.io": "^2.0.3" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'), 2 | http = require('http'), 3 | app = express(), 4 | server = http.createServer(app), 5 | io = require('socket.io').listen(server); 6 | app.get('/', (req, res) => { 7 | 8 | res.send('Chat Server is running on port 3000') 9 | }); 10 | io.on('connection', (socket) => { 11 | 12 | console.log('user connected') 13 | 14 | socket.on('join', function(userNickname) { 15 | 16 | console.log(userNickname +" : has joined the chat " ); 17 | 18 | socket.broadcast.emit('userjoinedthechat',userNickname +" : has joined the chat "); 19 | }); 20 | 21 | 22 | socket.on('messagedetection', (senderNickname,messageContent) => { 23 | 24 | //log the message in console 25 | 26 | console.log(senderNickname+" :" +messageContent) 27 | //create a message object 28 | let message = {"message":messageContent, "senderNickname":senderNickname} 29 | // send the message to the client side 30 | io.emit('message', message ); 31 | 32 | }); 33 | 34 | 35 | socket.on('disconnect', function() { 36 | console.log( ' user has left ') 37 | socket.broadcast.emit("userdisconnect"," user has left ") 38 | 39 | }); 40 | 41 | 42 | 43 | }); 44 | 45 | 46 | 47 | 48 | 49 | server.listen(3000,()=>{ 50 | 51 | console.log('Node app is running on port 3000'); 52 | 53 | }); 54 | --------------------------------------------------------------------------------