0

All I want to do isclient1 =(1)=> server =(2)=> otherclient =(3)=> server =(4)=> client1 however data cannot pass in =(4)=> back to the requester

server (node.js socket.io) `

    var data1 = "";
    socket.on("getdata", async(postid,cb)=>{ //==(1)==> //cb to client1 =(4)=>
        socket.broadcast.emit("sendreq",postid) //==(2)==>
        socket.on("callbacksendreq", (data)=>{ //==(3)==>

               //==(4)==> [option 1]//send to otherclient instead of client 1
            io.to(socket.id).emit("callbackgetdata", data) //socket.id is not client1

               //==(4)==> [option 2]//cb() not defined
            cb(data)//this function is from socket.on("getdata") but it doesnt work within another on
            data1 = data // data1 only change internally
        })
        cb(data1) //==(4)==>[option 3] but it return "" instead of data
                  //even though global scope, the data1 remain the same
    })

` client side //browser script (socket.io client from cdn)

    socket.on("connect",()=>{
        socket.emit("getdata", postid,(cbdata)=>{ //==(4)==>[option3][option2]
        Display(data);
        })
    })
    socket.on("callbackgetdata", (data)=>{ //==(4)==> [option2]
        Display(data);
    })

    //other client ask for comment //otherclient
    socket.on("sendreq",async(postid)=>{
        socket.emit("callbacksendreq", localStorage.getItem(postid) )
    })

I expect to sendrequest and get localStorage.getItem(postid) from other client as callback and display on HTML

Hey
  • 11
  • 3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – abondoa Oct 26 '22 at 21:29

0 Answers0