FiveM RPC
Call FiveM server, client and NUI listeners like async functions: typed, with timeouts, no event ping-pong
Motivation
The idea was to create an extensible package, with various features to simplify the development process and provide as much comfort as possible. Inspired by usage of altv-xrpc
Packages
| Package | Docs |
|---|---|
@entityseven/fivem-rpc |
API reference |
@entityseven/fivem-rpc-shared-types |
Typing setup |
Installation
npm i @entityseven/fivem-rpc
pnpm add @entityseven/fivem-rpc
yarn add @entityseven/fivem-rpc
bun add @entityseven/fivem-rpc
Optional, for typed event names, arguments and results (typing setup):
npm i -D @entityseven/fivem-rpc-shared-types
pnpm add -D @entityseven/fivem-rpc-shared-types
yarn add -D @entityseven/fivem-rpc-shared-types
bun add -d @entityseven/fivem-rpc-shared-types
Upgrading from 0.1: migration guide
Quick start
Create exactly one instance per environment and import it from your own module, not from the library. The client needs one even if it only relays between server and webview.
// server/rpc.ts
import { createRPC } from '@entityseven/fivem-rpc'
export const rpc = createRPC({ env: 'server' })
// client/rpc.ts
import { createRPC } from '@entityseven/fivem-rpc'
export const rpc = createRPC({ env: 'client' })
// webview/rpc.ts
import { createRPC } from '@entityseven/fivem-rpc'
export const rpc = createRPC({ env: 'webview' })
Listen on one side, emit from the other and await the listener's return value:
// server
rpc.onClient('ping', (player, message) => `pong: ${message} (from ${player})`)
// client
const reply = await rpc.emitServer('ping', 'hello')
All methods: API reference.
Features
- Type-Safe Development: Eliminate runtime errors and enhance code reliability with comprehensive type safety
- All-in-one package: Communicate effortlessly between server, client and webview
Contributing
Issues and pull requests are very welcome
Releases are published with bun publish, which replaces the workspace:^ dependency on shared-types with its version (npm publish would not)
License
Licensed under the Custom Attribution-NoDerivs Software License
Roadmap
- client observers to catch events between server and webview (subscribe-like behaviour)
- client observers to prevent events (middleware-like behaviour)
- player manager (transform player id to desired data straight from a listener)