Created
September 13, 2022 10:34
-
-
Save hieptl/d205c361c8c345033c8d695269d0015e to your computer and use it in GitHub Desktop.
Node.js Socket.io - chat.js - 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FILE /client/chat.js | |
console.log("chat.js file loaded!"); | |
// IMPORTANT! By default, socket.io() connects to the host that | |
// served the page, so we dont have to pass the server url | |
var socket = io.connect(); | |
//prompt to ask user's name | |
const username = prompt("Welcome! Please enter your name:"); | |
// emit event to server with the user's name | |
socket.emit("new-connection", { username }); | |
// captures welcome-message event from the server | |
socket.on("welcome-message", (data) => { | |
console.log("received welcome-message >>", data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment