Finally, Let's Send and Receive Message

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 396:- Finally, Let's Send and Receive Message

Great, let's start implementing sending and receiving messages in the chat room.

First, we need to modify the client-side JavaScript file chat.js. We'll add a function called sendMessage that will be called when the user clicks on the send button. This function will emit a message event to the server with the message text and the current room.

javascriptCopy code

function sendMessage() {  const messageInput = document.getElementById('message-input');  const message = messageInput.value;  socket.emit('message', { text: message, room: currentRoom });  messageInput.value = ''; }

Next, we need to modify the server-side code to handle the message event. We'll add the following code to the io.on('connection') callback:

javascriptCopy code

socket.on('message', (data) => {  const { text, room } = data;  const message = {    text,    sender: socket.username,    timestamp: new Date().getTime(),  };  io.to(room).emit('message', message); });

This code listens for the message event and extracts the message text and room from the data object. It then creates a new message object with the text, the sender (which is the username of the socket), and the current timestamp. Finally, it emits the message event to all sockets in the room.

Now we need to modify the client-side code to handle the message event. We'll add the following code to the socket.on('connect') callback:

javascriptCopy code

socket.on('message', (data) => {  const { text, sender, timestamp } = data;  const messageList = document.getElementById('message-list');  const messageElement = document.createElement('li');  messageElement.innerHTML = `<strong>${sender}</strong>: ${text} <span>${new Date(    timestamp  ).toLocaleTimeString()}</span>`;  messageList.appendChild(messageElement); });

This code listens for the message event and extracts the message text, sender, and timestamp from the data object. It then creates a new list item element and sets its innerHTML to the sender, message text, and timestamp. Finally, it appends the list item to the message list.

That's it! Now users can send and receive messages in the chat room.

46. Chatting Engine

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?