Skip to content

args[1] is received as null instead of socket acknowledgment callback #775

Open
@z-u-a

Description

@z-u-a

Problem:

Emitting event from server along with the callback and expecting client to trigger callback as the event received to acknowledge the server. Client is getting args[1] as null when it should be a callback.

Steps:
setup "socket.io-client": "^2.0.0" in react just to test if callback is emitting from server, in this test code, callback received.

server:

 const sendWithRetry = async (msg, attempts = 0, retries, options = {}) => {
    
    let timer;
    attempts++;
    const waitDelay = parseInt(options.waitDelay) || parseInt(global.settingss.socket_ack_wait_time) || 5000;
    const retryCount = parseInt(retries) || parseInt(global.settingss.socket_ack_failure_retries) || 3;

    const socket = socketIO.sockets.connected[msg.socketId];
    if(socket) {
        const response = await new Promise((resolve) => {

            timer = setTimeout(() => resolve(null), waitDelay); // Timeout occurred

            socket.emit(msg.eventName, msg.response, function(ack) {
                clearTimeout(timer);
                resolve(ack); // Resolve with client acknowledgment
            });
        });

        if (response && response.success) {
            return response; 
        } else if (!response && attempts < retryCount) {
            console.log(`No acknowledgment, retrying...`, attempts);
            clearTimeout(timer);
            return sendWithRetry(msg, attempts, retryCount, options); // Recursively retry
        } else {
            console.log('Acknowledgment not received after retries. sending push')
            clearTimeout(timer);
            sendPush(msg);
        }
    }}

client

        @Override
        public void call(Object... args) {
            Log.d("hello","helloTesting::"+new Gson().toJson(args));
            String serverResponse = args[0].toString();
            Utils.redLog(ApiTags.BOOKING_CONVERSION_NOTIFICATION, serverResponse);
            if (StringUtils.isNotBlank(serverResponse)) {
                try {
                    BookingConversion response = new Gson().fromJson(serverResponse, BookingConversion.class);
                    mCallback.onBookingConversion(response);
                }catch (Exception ignored){
                }
            }
            if (args.length > 1 && args[1] instanceof io.socket.emitter.Emitter.Listener) {
                Log.d("hello","helloTesting::1::"+new Gson().toJson(args[1]));
                Emitter.Listener ack = (Emitter.Listener) args[1];
                //ack.call("Acknowledgment received!");
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("success",true);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                ack.call(jsonObject);
            }
        }


server socket.io version "socket.io": "^2.0.3",
client version io.socket:socket.io-client:1.0.0

cannot upgrade versions as this is legacy code up and running on prod. Any help would be appreciated. Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions