rage-framework/rpc/readme.md

98 lines
2.5 KiB
Markdown
Raw Permalink Normal View History

2024-10-27 16:36:50 +00:00
# Rage-FW-RPC
is an all-in package with asynchronous RPC implementation for RageMP servers in JS/TS
# Installation
``` shell
npm i @entityseven/rage-fw-rpc
2024-10-27 16:36:50 +00:00
```
```shell
pnpm i @entityseven/rage-fw-rpc
2024-10-27 16:36:50 +00:00
```
```shell
yarn add @entityseven/rage-fw-rpc
2024-10-27 16:36:50 +00:00
```
Import installed package and initialize rpc:
```ts
2024-10-28 13:20:51 +00:00
// lib/rpc.js
2024-10-27 16:36:50 +00:00
2024-10-28 13:20:51 +00:00
import { Rpc } from 'rage-fw-rpc'
export const rpc = new Rpc(/* options */)
2024-10-27 16:36:50 +00:00
```
2024-10-28 13:20:51 +00:00
# Motivation
The idea was to create an extensible package, with various features to simplify the development process and provide as much comfort as possible. It should also be using similar architecture as the framework it was specially built for
Inspired by usage of [rage-rpc](https://github.com/micaww/rage-rpc)
2024-10-27 16:36:50 +00:00
# Features
- Type-safe events via [TS generics](https://www.typescriptlang.org/docs/handbook/2/generics.html), avoiding type wrappers
- Built-in logging options for each environment
- Error-safe developer mode for browser
- Calls can receive response from called environments via Promises (browser -> server -> browser, etc.)
- Actual human-readable errors
# Docs
2024-10-28 13:20:51 +00:00
## [Extended version available here](https://git.entityseven.com/entityseven/rage-framework/wiki/RPC%400.2.5)
2024-10-27 16:36:50 +00:00
## register
Registers a callback function for a specified event
```ts
rpc.register('playerJoin', (player) => {
console.log(`Connected: ${player.socialClub}`)
})
```
## unregister
Unregisters callback function for a specified event
```ts
rpc.unregister('playerDamage')
```
## callClient
Calls a client-side event from server or browser
From browser:
```ts
rpc.callClient('updatePlayerData', ['argument']).then(response => {
console.log(`Received: ${response}`)
})
```
From server (requires player):
```ts
rpc.callClient(player, 'updatePlayerData', ['argument']).then(response => {
console.log(`Received: ${response}`)
})
```
## callServer
Calls a server-side event from browser or client
```ts
rpc.callServer('updatePlayerData', ['argument']).then(response => {
console.log(`Received: ${response}`)
})
```
## callBrowser
Calls a server-side event from browser or client
From client:
```ts
rpc.callBrowser('updatePlayerData', ['argument']).then(response => {
console.log(`Received: ${response}`)
})
```
From client (requires player):
```ts
rpc.callBrowser(player, 'updatePlayerData', ['argument']).then(response => {
console.log(`Received: ${response}`)
})
```
## call
Calls an event in current environment
```ts
rpc.call('triggerSomething').then(response => {
console.log(`Received: ${response}`)
})
```