feat(es_extended/server/functions): add ESX.TriggerClientEvent #1342
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request introduces a new function
ESX.TriggerClientEvent
to the ESX framework. In the FiveM environment, the nativeTriggerClientEvent
is limited to triggering events for a single player ID at a time. This limitation incurs significant overhead when sending events to multiple clients, as the data must be re-serialized for each client. The new function addresses this issue by serializing the data once using msgpack and efficiently triggering events for multiple clients using the internal nativeTriggerClientEventInternal
.Changes Made
ESX.TriggerClientEvent
function to the ESX framework.TriggerClientEventInternal
.Motivation
In my experience, many scripts resort to inefficient practices when sending data to multiple clients. Some scripts send data to all clients to avoid the overhead of serialization, disregarding the principle that clients should only receive data relevant to them. Others use the
TriggerClientEvent
native for multiple clients, leading to significant performance implications such as server thread hitches. These practices are not only inefficient but can also degrade server performance and overall player experience.Introducing
ESX.TriggerClientEvent
aims to address these challenges by providing developers with a unified solution within the ESX framework. By serializing data once and efficiently triggering events for multiple clients using the internal native, this function ensures that clients receive only the data relevant to them while minimizing performance overhead. This not only promotes best practices in script development but also enhances the overall performance and stability of FiveM servers utilizing the ESX framework.Testing
As I am currently on vacation and unable to conduct testing, the implementation of theESX.TriggerClientEvent
function has not undergone direct testing. However, testing by a maintainer would be greatly appreciated to ensure the reliability and effectiveness of the new function. Alternatively, I will be back in a couple of days and can perform the testing myself.Has been tested locally on a single, and multiple clients. Work as expected.