esx-framework / esx_core

Official Repo For core resources for esx-legacy
https://documentation.esx-framework.org/
GNU General Public License v3.0
368 stars 733 forks source link

feat(es_extended/server/functions): add ESX.TriggerClientEvent #1342

Closed Kenshiin13 closed 5 months ago

Kenshiin13 commented 6 months ago

Description

This pull request introduces a new function ESX.TriggerClientEvent to the ESX framework. In the FiveM environment, the native TriggerClientEvent 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 native TriggerClientEventInternal.

Changes Made

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 the ESX.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.