rikulo / socket.io-client-dart

socket.io-client-dart: Dartlang port of socket.io-client https://github.com/socketio/socket.io-client
https://quire.io
MIT License
675 stars 185 forks source link

could we ack when we receive a new event #379

Open magamal opened 6 months ago

magamal commented 6 months ago

`socket.on("new_message",(data)=>{

//How to ack this event? })'`

jumperchen commented 6 months ago

Fyi, https://github.com/rikulo/socket.io-client-dart?tab=readme-ov-file#acknowledge-with-the-socket-server-that-an-event-has-been-received

magamal commented 6 months ago

Yes I read it before, but if I'm receiving a string from the server how I can ack this event

socket.on("new_message",(data)=>{ final String messageContent = data; }

steveroseik commented 6 months ago

Yes I read it before, but if I'm receiving a string from the server how I can ack this event

socket.on("new_message",(data)=>{ final String messageContent = data; }

Heyy, I had the same issue and figured out the solution.

It's pretty easy, it's the lack of information in the documentation that makes it hard to figure out.

I'll show you my example:

in my server side I send a nudge to the client, when you send it, the client receives a list of data, consists of the data you sent in the emit and an extra callback function that should be used for acks:

so this is how the data might look like if you used my code below:

['Input the data to be sent.', function()]

this.server
        .to(opponentId)
        .timeout(5000)
        .emit(
          'nudge',
          'Input the data to be sent.',
          (err, responses) => {
            if (err) {
              /// handle timeout
            } else {
              // handle responses
            }
          },
        );

That's why in flutter you'll need to treat the data received as a list, and extract from it both data sent from server and the function;

you can handle the data as you like, and the take the last item as Function and run it as described in their code sample:

socket.on('nudge', (data) {
    final dataList = data as List;

    final customData = dataList.first;

    final ack = dataList.last as Function;
    ack(null);
});
makamekm commented 1 month ago

It stopped working in version 3. Rolled back to 2.

Uncaught (in promise) DartError: NoSuchMethodError: ''
Dynamic call with too many positional arguments. Expected: 2 Actual: 3
Receiver: Instance of '(String, dynamic) => Null'
Arguments: ["report", Instance of '_JsonMap', Instance of '(dynamic) => Null']
    at Object.throw_ [as throw] (errors.dart:296:3)
    at Object.defaultNoSuchMethod (operations.dart:913:3)
    at Function.noSuchMethod (core_patch.dart:63:17)
    at Object.noSuchMethod (operations.dart:888:31)
    at callNSM (operations.dart:318:12)
    at Object._checkAndCall (operations.dart:428:10)
    at Object.dcall (operations.dart:451:39)
    at Function.apply (core_patch.dart:103:17)
    at socket$0.Socket.new.emitEvent (socket.dart:457:18)
    at socket$0.Socket.new.onevent (socket.dart:447:7)
    at socket$0.Socket.new.onpacket (socket.dart:412:9)
    at Object._checkAndCall (operations.dart:426:37)
    at Object.dcall (operations.dart:451:39)
    at event_emitter.dart:44:14
    at [dartx.forEach] (js_array.dart:212:7)
    at [_emit] (event_emitter.dart:43:11)
    at manager.Manager.new.emitReserved (event_emitter.dart:57:47)
    at manager.dart:276:7
    at async._AsyncCallbackEntry.new.callback (future.dart:285:40)
    at Object._microtaskLoop (schedule_microtask.dart:40:11)
    at _startMicrotaskLoop (schedule_microtask.dart:49:5)
    at async_patch.dart:181:7
jumperchen commented 1 month ago

@makamekm Could you provide a reproducible example?