Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This project is based on an open source Node.js application posted at: http://socket.io/get-started/chat/. Your job is to modify the index.js page so that the app

This project is based on an open source Node.js application posted at: http://socket.io/get-started/chat/. Your job is to modify the index.js page so that the app will have two new features:

Whenever someone joins or leaves the room, a message will be displayed to all users.

Instead of having a username, each user is assigned a socket generated session id when he/she first joins.

index.js

var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); io.on('connection', function(socket){ socket.on('chat message', function(msg){ io.emit('chat message', msg); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); }); 

You can download this project source code from https://github.com/rauchg/chat-example

When a user joins the chat room, you need to register the connect event. For example:

io.on(connect, function(socket)) {

io.emit(chat message, --- someone just joined the room!!!);

});

When a user leaves the chat room, you need to register the disconnect event on that particular socket. For example:

io.on(connection, function(socket) {

socket.on(disconnect, function() {

io.emit(chat message, --- someone just left the room!!!);

});

});

To get the session Id from each user, you can use socket.client.id property. For example:

io.emit(chat message, --- + socket.client.id + just joined the room!!!);

You only need to submit the modified index.js file.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions

Question

Understand the importance of strategic HR planning.

Answered: 1 week ago