Home > Blockchain >  socket.io console.log(userName) not showing in the command line
socket.io console.log(userName) not showing in the command line

Time:06-19

I'm creating a chat application using socket.io, so basically what I'm trying is to console.log the user who joined the chat, here I'm taking a prompt from the client and emitting to the server, but cannot find any log in my command line. And on top I'm getting this error ERR_NAME_NOT_RESOLVED.

ERR_NAME_NOT_RESOLVED img Index Html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>See-me</title>
        <script src="http://localhost:3000/socket.io/socket.io.js"></script>
        <script src="js/client.js"></script>
        <link rel="stylesheet" href="css/styles.css">
        <link rel = "icon" href ="logo/appIcon.ico" type = "image/x-icon">
</head>
<body>
   
</body>
</html>

Client Side Js:

    const socket = io('http//localhost:3000')

    const form = document.getElementById('send-container')
    const messageInput = document.getElementById('messageImp')
    const messageContainer = document.querySelector(".container")

const userName = prompt("Enter your Name to join");
socket.emit('new-user-joined', userName)

Server Side JS:

const io = require('socket.io')(3000)

const users = {};

io.on('connection', (socket) => {
    socket.on('new-user-joined', userName =>{
        console.log("New user", userName);
        users[socket.id] = userName;
        socket.broadcast.emit('user-joined', userName)
    });

    socket.on('send', message => {
        socket.broadcast.emit('receive', {message: message, userName: users[socket.id]})
    });
})

All I want is to console log the user who joined the chat.

CodePudding user response:

The error is actually pointing to what the issue is:

const socket = io('http//localhost:3000')

That URL is missing a colon after http

  • Related