Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Socket IO connection issue in NodeJS

I am trying to make a socket connection in NodeJS App. I tried to run this code

index.js


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

const users = {};

io.on('connection', socket =>{

    socket.on('new-user-joined', name =>{ 

        users[socket.id] = name;

        socket.broadcast.emit('user-joined', name);
    });

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

 
    socket.on('disconnect', message =>{
        socket.broadcast.emit('left', users[socket.id]);
        delete users[socket.id];
    });
});

client.js

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

socket.on('user-joined', user =>{
    append(`${user} joined the chat`, 'right')
})

socket.on('receive', data =>{
    append(`${data.name}: ${data.message}`, 'left')
})

socket.on('left', user =>{
    append(`${user} left the chat`, 'right')
})

But I am not getting correct results and any errors in console.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

<script src="/socket.io/socket.io.js"></script>
<script>

var socket = io();


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

socket.on('user-joined', user =>{
    append(`${user} joined the chat`, 'right')
})

socket.on('receive', data =>{
    append(`${data.name}: ${data.message}`, 'left')
})

socket.on('left', user =>{
    append(`${user} left the chat`, 'right')
})

please get a socket connection in HTML.

Which problem you face please briefly explain. Thankyou.

</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!