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

136
Views
Why the sender recieve the socket.broadcast.emit event also?

Here is my server code:

require('dotenv-flow').config();
const express = require('express');
const app = express();
const http =require('http');
const httpServer= http.createServer(app);
const io = require('socket.io')(httpServer, {
    allowEIO3: true
});
let Test=require('./test/Test');

httpServer.listen(process.env.REACT_APP_SOCKET_PORT, () =>{
  console.log('Express server is running on localhost:'+process.env.REACT_APP_SOCKET_PORT);
});
io.of("/test").on("connection",(socket)=>{
    let test=new Test(socket);
})

Test.js:

class Test{
    constructor(socket){
        console.log("Test:Connection established");
        socket.on("askConnect",()=>{
            console.log("Test:askConnect event received.")
            socket.broadcast.emit("requestConnect", {});
        });
    }    
}
module.exports=Test;

And my client code:

import io from 'socket.io-client';
export default function A(){
    let peerName;
    let sUsrAg = navigator.userAgent;
    let socket = io.connect(process.env.REACT_APP_SOCKET_URL+"test", { transports: ['websocket'] });
    socket.on("requestConnect",()=>{
        console.log(peerName+' received request connect event');
    })
    if (sUsrAg.indexOf("Chrome")>-1){
        peerName="Chrome";
    }else {
        peerName="Firefox";
    }
    let go=()=>{
        socket.emit('askConnect');
    }
    return(
        <button onClick={go}>Go</button>
    ); 
}

I browse the web page with 2 browsers.

I expect when I click the "go" button, the socket.on("requestConnect",...) should be triggered in the other browser only.

In fact, the socket.on("requestConnect",...) be triggered in the both browser.

What's happening?

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

0

After several tries, when I changed the client code as below, the sender does not receive socket.on("requestConnect",...) event again.

import io from 'socket.io-client';
import { useEffect,useState } from 'react';
export default function A(){
    let peerName;
    const[socket,setSocket]= useState();
    let sUsrAg = navigator.userAgent;    
    if (sUsrAg.indexOf("Chrome")>-1){
        peerName="Chrome";
    }else {
        peerName="Firefox";
    }
    useEffect(()=>{
        let temp=io.connect(process.env.REACT_APP_SOCKET_URL+"test", { transports: ['websocket'] });
        temp.on("requestConnect",(remotePeerName)=>{
            console.log('Received request connect event from '+remotePeerName);
        })
        setSocket(temp);
    },[])
    let go=()=>{
        socket.emit('askConnect',peerName);
    }
    return(
        <button onClick={go}>Go</button>
    ); 
} 

But I don't know why.

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!