Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
z-u-a opened this issue Nov 28, 2024 · 0 comments
Open

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

z-u-a opened this issue Nov 28, 2024 · 0 comments
Labels

Comments

@z-u-a
Copy link

z-u-a commented Nov 28, 2024

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

@z-u-a z-u-a added the question label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant