lib/rpc
RPC system for client/server communication via the SWC modules plugin.
This module provides the global.rpc implementation that the SWC modules plugin
uses for transformed method calls. It bridges FiveM's native event system with
a promise-based RPC interface.
Architecture
The RPC system uses FiveM's emitNet/onNet for cross-environment communication:
Client Server
─────── ──────
callServer(event, ctx) ─────────────► onNet handler
│ │
│ ◄───────────── emitNet response
▼
Promise resolves
Event Naming
RPC events use the format: ClassName:methodName@moduleId
The moduleId provides instance-level namespacing, allowing multiple instances of the same class to coexist without event name collisions.
Trace Context Propagation
The RPC system automatically propagates trace context between client and server. Each RPC call creates a child span and includes the trace context in the payload. This enables distributed tracing across the FiveM client/server boundary.
Usage
The RPC system is initialized automatically by calling initRpc() in the
runtime bootstrap. Users interact with it indirectly through decorated methods.
Interfaces
| Interface | Description |
|---|---|
| PendingCall | Internal type for pending RPC calls. |
| RegisteredHandler | Internal type for registered handlers. |
| RpcInterface | The global RPC interface used by the SWC modules plugin. |
| TracedPayload | Wraps an RPC payload with trace context for propagation. |
Type Aliases
| Type Alias | Description |
|---|---|
| RpcHandler | RPC handler function type. Receives the event context and returns a promise with the result. |
Functions
| Function | Description |
|---|---|
| createModuleContext | Creates a module context for RPC registration. The moduleId must be deterministic (same on client and server) for RPC to work. |
| getHandlerCount | Get registered handler count (for debugging). |
| getPendingCallCount | Get pending call count (for debugging). |
| handleRpcResponse | Handle RPC responses. Exported for use by platform-specific RPC init (client NUI callback, web message handler). |
| initRpc | Initialize the RPC system. Must be called during runtime bootstrap before any modules start. |
| registerClientHandler | Register an RPC handler on the client (for @Client decorated methods). |
| registerDuiHandle | Register a DUI handle to receive callWeb messages. |
| registerServerHandler | Register an RPC handler on the server (for @Server decorated methods). |
| shutdownRpc | Shutdown the RPC system. |
| unregisterDuiHandle | Unregister a DUI handle. |
References
handleNuiRpcResponse
Renames and re-exports handleRpcResponse