Step 4: To listen for broadcasted chat messages, go
to index.html, and include a script from the socket.io package in the
head:
<script src="/socket.io/socket.io.js"></script>
Go to script.js, and add the following near the top:
let socket = io()
The
socket
object can be used to listen for broadcasted
data from the server. To listen for broadcasted chat messages, add
the following below getting the chat messages:
socket.on(`message`, showMessage)
The function
showMessage
will get called when the server
broadcasts a message. Add the following at the bottom:
function showMessage(message) {
}
Fill in the function to show the message on the page.