Search code examples
c#node.jswebsocketsocket.io

Socket.IO Callback not working in (Node/C#)


I am so concern about an issue that i tried 100 solutions for but nothing works.

In python i can :

self.sio.emit('join', 'join', callback=self.join_acknowledgment)

and it works with charms. I.e. Whenever it fired a join event, server in return sends some data on to this function. Similarly i can do the same in dart without any issue i.e.

 _socket!.emitWithAck(
      "join",
      "join",
      ack: (data) {/*to do with data*/},);

But in C# sockets.io / WebSockets /SocketsSharp, you name it, i can't achieve this in any particaally possible way!. Can you help?

 private void OnOpen(object sender, EventArgs e)
    {
        Console.WriteLine("WebSocket opened");
        string uniqueId = Guid.NewGuid().ToString();
        var joinData = new JObject
        {
            { "join", "join" },
            { "id", uniqueId },
        };

        string joinMessage = $"42[\"join\", {joinData.ToString(Formatting.None)}]";
        Console.WriteLine($"Sending: {joinMessage}");
        // Store the callback for later use
        callbacks[uniqueId] = (response) =>
        {
            Console.WriteLine("Join acknowledged with data:");
            Console.WriteLine(response);
        };
        webSocket.Send(joinMessage);
    }

I tried everything including shared code above, i tried it in SocketIO/WebSocket/SocketSharp. I can not change the code of node.js as it works 100% fine for python and flutter(dart)

Node.js Server Side Code:

    private onConnect(): void {
    global.io.on('connection',async (socket: any) => {
        let id: string = socket.request['id']
        console.log("connected" + id);
        socket.on('join', async (args, callback) => {
            console.log('Join event received:', args);
            console.log(callback);
            //get some data from db <<<
            var mydata = getSomeDataFromDB();
            callback(resOK(mydata))
        })
    })
}

Solution

  • For those who may encounter this issue in C#. I have found the solution to the issue myself.

    While reading posts online and documentation of SocketIO and EngineIO, i thought i'd try something that atleast i did not find written in any threads.

    My original message was: string joinMessage = $"42[\"join\",{joinData.ToString(Formatting.None)}]";

    And i expected it to trigger: socket.on('join', async (args, callback) => {

    Join always got fired and i got args as well but callback was always empty/undefined. To solve this i chagned the joinMessage to:

    string joinMessage = $"423[\"join\", {joinData.ToString(Formatting.None)}]";

    The 1st digit 4 specifies to treat the whole thing as message. 2nd digit 2 specifies that it is an event i.e. 'join'. The 3rd one that i added for hit and trial that worked, specifies that ACK is also sent, making it emitWithAck funciton.

    I hope this helps someone as i tried so many solutions but at the end this worked.

    Ref: This helped as well