Get response from server to client


#1

Hi,

I’d like to get data from database to my client. What is the best way to do that?

Thanks for your help

P


#2

Get the data from your database on the serverside, then use a client event to pass it across.


#3

First, thank you for your help!

I did that but I was wudering if it was possible to do somthing like

myVar = TriggerServerEvent('my:serverFunction', 'myFilterParam')

Or using a call back

TriggerServerEvent('my:serverFunction', 'myFilterParam', function(myResultSet)
    -- do somthing
end)

It’s because the call must be used in different context with different values to filter the query


#5

Here how I did it finally:

-- client.lua
RegisterNetEvent("ReceiveDbInfo")
AddEventHandler("ReceiveDbInfo", function (value)
	TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, 'Command ReceiveDbInfo received value: ' .. value)
end)
--server.lua
TriggerEvent('es:addCommand', 'getdbinfo', function(source, args, user)
	TriggerEvent('GetBbInfo', source)
end)

RegisterServerEvent("GetBbInfo")
AddEventHandler("GetBbInfo", function(source)
	-- This is where you get the information from the database
	local responseFromDB = "Response from database"

	TriggerClientEvent('ReceiveDbInfo', source, responseFromDB)
end)