RAGE.MP - C# Serverside Events to Typescript Client-side Events

Rage.MP is a GTA V modification that allows you to write / create / run custom servers. I was programming a server using the Rage.MP API in the past and I'm going to try and assist any of you whom are interested in trying to learn programming while using the Rage.MP API.

The hardest thing to figure out was how server side code makes the connection with client side code. Events to say the least.

Now having not touched programming in over 7 months it took me a bit of research and asking around to get the answers and mindset I very much needed. Here's what you need to know.

Server side: (C#)

NAPI.ClientEvent.TriggerClientEvent(client, "yourEventNameHere");

Okay wait that's pretty fucking vague. Here's a real look at where it could belong.

[ServerEvent(Event.ChatMessage)]
public void chatTesting(Client client, string message) {  
    if (message == "hello") {
        NAPI.ClientEvent.TriggerClientEvent(client, "hello");
    }
}

Okay so what is happening here is that when a player speaks in chat and they say 'hello' it will trigger an event on the player. The player is defined as client in the above code. When this occurs the client side code takes over.

We can catch the code from the server with something such as:

Client side: (TS / JS)

mp.events.add('hello', () => {  
    mp.gui.chat.push("Hello World!");
});

Once we catch the event that is called hello it tells the client-side code to push out a chat message JUST to the client who was sent an event. Thus we receive the text 'Hello World' in chat.

In those regards that's just the basics of pushing events from Server to Client.