Skip to main content

--description--

Many chat rooms are able to announce when a user connects or disconnects and then display that to all of the connected users in the chat. Seeing as though you already are emitting an event on connect and disconnect, you will just have to modify this event to support such a feature. The most logical way of doing so is sending 3 pieces of data with the event: the username of the user who connected/disconnected, the current user count, and if that username connected or disconnected.

Change the event name to 'user', and pass an object along containing the fields username, currentUsers, and connected (to be true in case of connection, or false for disconnection of the user sent). Be sure to change both 'user count' events and set the disconnect one to send false for the field connected instead of true like the event emitted on connect.

io.emit('user', {
username: socket.request.user.username,
currentUsers,
connected: true
});

Now your client will have all the necessary information to correctly display the current user count and announce when a user connects or disconnects! To handle this event on the client side we should listen for 'user', then update the current user count by using jQuery to change the text of #num-users to '{NUMBER} users online', as well as append a <li> to the unordered list with id messages with '{NAME} has {joined/left} the chat.'.

An implementation of this could look like the following:

socket.on('user', data => {
$('#num-users').text(data.currentUsers + ' users online');
let message =
data.username +
(data.connected ? ' has joined the chat.' : ' has left the chat.');
$('#messages').append($('<li>').html('<b>' + message + '</b>'));
});

Submit your page when you think you've got it right. If you're running into errors, you can check out the project completed up to this point .

--hints--

Event 'user' should be emitted with username, currentUsers, and connected.

async (getUserInput) => {
const url = new URL("/_api/server.js", getUserInput("url"));
const res = await fetch(url);
const data = await res.text();
// Regex is lenient to match both `username` and `name` as the key on purpose.
assert.match(
data,
/io.emit.*('|")user\1.*name.*currentUsers.*connected/s,
'You should have an event emitted named user sending name, currentUsers, and connected'
);
}

Client should properly handle and display the new data from event 'user'.

async (getUserInput) => {
const url = new URL("/public/client.js", getUserInput("url"));
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/socket.on.*('|")user\1[^]*num-users/s,
'You should change the text of "#num-users" within on your client within the "user" event listener to show the current users connected'
);
assert.match(
data,
/socket.on.*('|")user\1[^]*messages.*li/s,
'You should append a list item to "#messages" on your client within the "user" event listener to announce a user came or went'
);
}