Skip to content

Instantly share code, notes, and snippets.

@hieptl
Created September 13, 2022 10:34
Show Gist options
  • Save hieptl/d205c361c8c345033c8d695269d0015e to your computer and use it in GitHub Desktop.
Save hieptl/d205c361c8c345033c8d695269d0015e to your computer and use it in GitHub Desktop.
Node.js Socket.io - chat.js - 2
// 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