Events
Events work in a very specific way and understanding their communication is very important.
The server can communicate with any client. Clients may only communicate with WebViews and the server.
A client CANNOT communicate with another client.
Function Name | Description |
---|---|
alt.emit | Emit an event on server or client side. Only received on the side it was emitted from. |
alt.on | Receives an event. Server only receives server events. Client only receives client events. |
alt.off | Stop listening to custom or built-in events. |
alt.onServer | Receives an event emitted from the server on client-side. Triggered with alt.emitClient . |
alt.offServer | Stop listening to custom events from the server. |
alt.emitClient | Emit an event to a specific client or an array of clients that they receive with alt.onServer . |
alt.emitClientRaw | Emit an event to a specific client or an array of clients. It works faster than emitAllClients that they receive with alt.onServer . |
alt.emitAllClients | Emit an event to all clients that they receive with alt.onServer . |
alt.emitAllClientsRaw | Emit an event to all clients. It works faster than emitAllClients that they receive with alt.onServer . |
alt.onClient | Receives an event emitted from the client on server-side. Triggered with alt.emitServer . |
alt.offClient | Stop listening to custom events from the client. |
alt.emitServer | Emit an event to the server that is received with alt.onClient . |
alt.emitServerRaw | (only works, server is using javascript) Emit an event to the server. It works faster than emitServer that they receive with alt.onClient |
Server to client
The server may only emit data to the client-side with emitClient
or emitClientRaw
, which requires a Player.
emitClientRaw
and emitAllClientsRaw
are used for big data and are faster than emitClient
or emitAllClients
.
alt.onServer('sayHello', () => {
alt.log('Hello from server.');
});
Client to server
The client may only emit data to the server-side with emitServer
or emitServerRaw
.
emitServerRaw
is used for big data and is faster than emitServer
and emitServerRaw
only works, if the server is using javascript.
The server-side onClient
event handlers will automatically receive the player that sent the event as the first argument.
alt.on('connectionComplete', () => {
alt.emitServer('sayHello');
});
Server resource to server resource
The server & client may only communicate with itself with the on
and emit
functions.
They are sent and received across resources as well.
alt.emit('hello', 'this is a message');
alt.on('hello', msg => {
alt.log(msg);
});
Client resource to client resource
alt.emit('hello', 'this is a message');
alt.on('hello', msg => {
alt.log(msg);
});
Client to WebView and back
Note: resource
in the URL of the WebView refers to the resource that you are currently writing code for.
const webview = new alt.WebView('http://resource/client/html/index.html');
webview.on('test2', handleFromWebview);
function handleFromWebview(msg) {
alt.log(msg);
}
alt.setTimeout(() => {
webview.emit('test', 'Hello from Client');
}, 500);
Unsubscribes from events
alt.on('customEventName', customEventFunction);
function customEventFunction() {
alt.off('customEventName', customEventFunction);
}
Raw Events
Raw events serialize the values to their raw byte buffer representation instead of converting them to an intermediate value, because of this the raw events can serialize and send big objects / arrays a lot faster than the normal events, BUT the downside is that this only works when both sides are using JavaScript.
Think of them as "native" JavaScript events, they can send almost any JS built-in data structure, such as Set
, Map
, Date
, etc. and without limits (for example, BigInt of any size).
Caution
alt.emitServerRaw
only works if the server is using javascript.
- Server-side(emitClientRaw or emitAllClientsRaw)
- Client-side(emitClientRaw or emitAllClientsRaw listener)
- Client-side(emitServerRaw)
- Server-side(emitServerRaw listener)
alt.on('playerConnect', player => {
const array = [
{ id: 0, name: "test1" },
{ id: 1, name: "test2" }
]
alt.emitClientRaw(player, 'sayHello', array); // Send an event to a specific player
// Send an event to all players
alt.emitAllClientsRaw('sayHello', array);
});