Skip to main content

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

InterfaceDescription
PendingCallInternal type for pending RPC calls.
RegisteredHandlerInternal type for registered handlers.
RpcInterfaceThe global RPC interface used by the SWC modules plugin.
TracedPayloadWraps an RPC payload with trace context for propagation.

Type Aliases

Type AliasDescription
RpcHandlerRPC handler function type. Receives the event context and returns a promise with the result.

Functions

FunctionDescription
createModuleContextCreates a module context for RPC registration. The moduleId must be deterministic (same on client and server) for RPC to work.
getHandlerCountGet registered handler count (for debugging).
getPendingCallCountGet pending call count (for debugging).
handleRpcResponseHandle RPC responses. Exported for use by platform-specific RPC init (client NUI callback, web message handler).
initRpcInitialize the RPC system. Must be called during runtime bootstrap before any modules start.
registerClientHandlerRegister an RPC handler on the client (for @Client decorated methods).
registerDuiHandleRegister a DUI handle to receive callWeb messages.
registerServerHandlerRegister an RPC handler on the server (for @Server decorated methods).
shutdownRpcShutdown the RPC system.
unregisterDuiHandleUnregister a DUI handle.

References

handleNuiRpcResponse

Renames and re-exports handleRpcResponse