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

412
Views
How can I disconnect clients with socket io?

I am trying to write an application in which clients share real-time location with each other with javascript. It closes on the server.js side, but I can't see it on the client side of the client.

Server.js

const server = require('http').Server(app);
const io = require('socket.io')(server);
server.listen(8080)
const location =[];
const players ={}

io.on('connection',(socket)=>{
    socket.on('Client_send_pos',(data)=>{ /* position verisi alınır. */
    console.log("new client connected, with id " + socket.id);
    location.push(data); /* location dizine aktarılır.*/
    io.emit('send_mang_pos',location); /* emit edilerek yazdırılması sağlanır. */             
});
        
socket.on("disconnect",() => {
    console.log(`${data} User disconnected.`)
    const index = location.indexOf(data); /* disconnect olan browser'ın dizide kaçıncı eleman olduğu bulu. */
    if (index != -1) {
        delete location[index];
        location.splice(index, 1);
        io.emit('delete',index);
        console.log(index);
    }
    io.emit('send_mang_pos',location);           
});

Client

let socket = io.connect('http://localhost:8080');
const position = [this.state.location.x, this.state.location.y]
socket.emit('Client_send_pos',position)
const map = L.map('map').setView(position, 10);
        
socket.on('send_mang_pos',(data)=>{
    data.forEach((i,y,z)=>{
                
    });
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I've written something similar: https://github.com/armonkahil/Dismissed/blob/master/client/src/components/Buttons/index.js It was my understanding that socket.io handled that itself. When the client leaves, it will emit a "disconnect" event. I never wrote a disconnect emitter. I just made sure to listen for it on the server-side like you already have set up.

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!