Callbacks sent from the server for client not working

Hello :slightly_smiling_face:
callbacks sent from the server for client not working

I have this server artifacts => https://runtime.fivem.net/artifacts/fivem/build_server_windows/master/819-c026422393b41af81102dc3c95e5e4ebf3793477/

and i have fresh updated my cfx-server-data

Have exactly used the same code of this pull request this does not work and returns to me this error
(https://github.com/citizenfx/cfx-server-data/pull/31)

Error running system event handling function for resource pyta_roleplay: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: citizen:/scripting/lua/MessagePack.lua:830: missing bytes
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/MessagePack.lua:830: in method 'underflow'
        citizen:/scripting/lua/MessagePack.lua:465: in field 'any'
        citizen:/scripting/lua/MessagePack.lua:860: in field 'unpack'
        citizen:/scripting/lua/scheduler.lua:602: in local 'cb'
        main/tools/server/event.lua:13: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:195: in function <citizen:/scripting/lua/scheduler.lua:194>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:41: in field 'CreateThreadNow'
        citizen:/scripting/lua/scheduler.lua:194: in function <citizen:/scripting/lua/scheduler.lua:158>

Send your full code.

client.lua

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1)
        if IsControlJustPressed(1, 38) then
            TriggerServerEvent("callbackTest", function(val)
                Citizen.Trace(val) --would never get called
            end)
        end
    end
end)

server.lua

RegisterNetEvent('callbackTest')
AddEventHandler('callbackTest', function(cb)
  cb("test")
end)

Try replacing your server code with this:

RegisterServerEvent('callbackTest')
AddEventHandler('callbackTest', function(cb)
    cb("test")
end)

I get the same error :roll_eyes:

Not sure :man_shrugging: . Sorry, you’ll have to wait for someone else to respond.

No problem ! Thank you for your help :slightly_smiling_face:

1 Like

You cant do callbacks like that going from client to server or server to client.

If you wanna do callbacks like ESX they created the callback logic manually I believe.

2 Likes

Are you sure ? Because this person (https://github.com/citizenfx/cfx-server-data/pull/31) does what I would like to do

I could never get them to work like that. I am not sure why ESX would create the logic to manually handle event callbacks if it was as easy as the example that guy committed. Which leads to me believing these guys are manually handling the logic in some form. In the example of that guys commit I don’t even see in the sessionmanager a callback being called from the client to the server it seems the client file is empty?

Maybe they have found a way to make it work I honestly would like to find that out myself. I am just going off of what I tested when I messed with it a while back.

1 Like

I don’t know why ESX has created a callback management in its system. I’ve never been used, ESX,

Yes, the customer file is empty. :sweat_smile:

I spoke with the person who made this pull request he confirmed to me that this was a bug and my advisor to report it on the forums. :roll_eyes:

HM. Interesting. Keep me update would you if you find anything?

Yep :slightly_smiling_face:

2 Likes

By default , I think you can’t callback between server and client, because cb client doesn’t exist on server side, so it can’t call a non existing function.

I’ll look if there’s any release for that, if not, i’ll do it myself :wink:

1 Like

The ESX callback implementation is actually pretty good. Might be a decent starting point.

Client: https://github.com/ESX-Org/es_extended/blob/master/client/functions.lua#L68

Server: https://github.com/ESX-Org/es_extended/blob/master/server/functions.lua#L31

I would love to see this or something similar baked into the server artifacts at some point. Callbacks to the client are ridiculously useful.

2 Likes

… does nobody get that this is already implemented as a built-in thing, transparently? The topic is to report that in some specific case it regressed, not a feature request nor a question asking ‘hey can I do this’.

2 Likes

I hadn’t seen it documented anywhere, except for the PR mentioned above, so I had no indicator it was a baked in feature. I’d be stoked to see it fixed and I can transition to a native callback function.

1 Like

after a lot of testing, i came to the conclusion this is when it all started happening, which also makes sense since this commit touches funcrefs and all that stuff.

really didn’t need ‘a lot of testing’ to find out the obvious now, but of course you’re an idiot so you like to sound pretentious.

2 Likes

What’d potentially work as a fix would be to deserialize events before sending to a remote party, check if there are any funcrefs in the event, and add a reference to these funcrefs.

This’d lead to memory leaks of any closure involving these funcrefs, but that can’t be resolved without some insanely convoluted distributed reference counting (or making ScRTs responsible for notifying the engine when a reference count drops to zero, and adding a reference for every event target client?).

Of course, that’s assuming the issue here is that the funcref gets freed before getting a chance to be called. :confused:

2 Likes