Skip to content

Asynchronous Remote Procedure Call (altv-rpc)

Notifications You must be signed in to change notification settings

mavdol/altv-rpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 

Repository files navigation

alt:V - asynchronous remote procedure call (altv-rpc)

A minimalist RPC designed for alt:V

Installation

alt:V rpc need to be included in every resource in which you want to use RPC system!

client

  1. Download rpc files
  2. Put "src" the folder in your ressource
  3. Include folder in resource.cfg file
  4. Finally import RPC inside the client
import rpc from 'yourpath/rpc.mjs';

server

  1. Download rpc files
  2. Put "src" the folder in your ressource
  3. Import RPC inside your serve file
import rpc from 'yourpath/rpc.mjs';

browser

  1. Just have to link javascript file "rpc.browser.mjs"
<html>
    <head>
        <title>My Page</title>
        <script type="text/javascript" src="./rpc.browser.mjs"></script>
        <script type="text/javascript">
            rpc.register('peace', () => 'hello from browser!');
            
            // ...
        </script>
    </head>
</html>

ps: browser rpc system requires to instantiate view via createView(name, url) to use rpc functions.

Usage

Universal

register(name, callback)

Used to register an procedure in the current context that will be call after.

parameters
  • name string - The unique identifier, relative to the current context, of the procedure.
  • callback function - The procedure. This function will receive 1 arguments.
    • arg The arguments that were provided by the caller.
Exemples
rpc.register('sayhello', () => 'hello')

Returns 'hello' to the caller asynchronously.


rpc.register('getUser', async (id) => {
    const user = await getUserFromId(id);
    return user;
});

Returns the resolved user to the caller.

unregister(name, callback)

unregister an procedure in the current context.

parameters
  • name string - The unique identifier, relative to the current context, of the procedure.
Exemples
rpc.unregister('sayhello')

call(name, callback)

call a procedure in the current context.

parameters
  • name string - The unique identifier, relative to the current context, of the procedure.
  • callback function - The procedure. This function will receive 1 arguments.
    • arg The arguments that were provided by the caller.
Exemples
rpc.register('sayhello', () => 'hello');

rpc.call('sayhello').then((result) => {
    console.log(result)
    // result return hello
})

Server side

callClient(name, args?)

call a procedure register in client context.

parameters
  • player Player - the instance of the player who is targeted.
  • name string - The unique identifier, relative to the current context, of the procedure.
  • args Optional arguments to pass to the procedure.
Exemples

client

rpc.register('sayhello', () => 'hello');

server

rpc.callClient(player,'sayhello').then((result) => {
    console.log(result)
    // result return hello
}).catch(err => {
    // if name doesn't exist in client context
    // handle error 
});

callBrowser(player, viewname, name, args?)

call a procedure register in browser context.

parameters
  • player Player - the instance of the player who is targeted.
  • viewname string - The unique identifier, relative to view declared before.
  • name string - The unique identifier, relative to the current context, of the procedure.
  • args Optional arguments to pass to the procedure.
Exemples

browser

rpc.register('myevent', () => '42');

server

rpc.callBrowser(player, 'myview', 'myevent').then((result) => {
    console.log(result)
    // result return 42
}).catch(err => {
    // if name doesn't exist in client context
    // handle error 
});

Client side

callServer(name, args?)

call a procedure register in server context.

parameters
  • name string - The unique identifier, relative to the current context, of the procedure.
  • args Optional arguments to pass to the procedure.
Exemples

server

rpc.register('myevent', () => '42');

client

//in server file
rpc.callServer('myevent').then((result) => {
    alt.log(result)
    // result return 42
}).catch(err => {
    // if name doesn't exist in server context
    // handle error 
});

callBrowser(viewname, name, args?)

call a procedure register in browser context.

parameters
  • viewname string - The unique identifier, relative to view declared before.
  • name string - The unique identifier, relative to the current context, of the procedure.
  • args Optional arguments to pass to the procedure.
Exemples

browser

rpc.register('myevent', () => '42');

client

rpc.callBrowser(player, 'view1', 'myevent').then((result) => {
    alt.log(result)
    // result return 42
}).catch(err => {
    // if name doesn't exist in client context
    // handle error 
});

createView(name, url)

create an view instance. required to use rpc function in browser files

parameters
  • name string - The unique identifier of selected view.
  • url string - the url to have acces to your view.
Exemples
let view = rpc.createView('myView', 'http://resource/myUrl');

return a view instance

destroyView(name)

rpc.detroyView('myView');

destroy view

getView(name)

let view = rpc.getView('myView');

return view instance

getViews()

let viewsArray = rpc.getViews();

return all view instance in an array

Browser side

callClient(name, args?)

call a procedure register in client context.

parameters
  • name string - The unique identifier, relative to the current context, of the procedure.
  • args Optional arguments to pass to the procedure.
Exemples

client

rpc.register('myevent', () => '42');

browser

rpc.callClient('myevent').then((result) => {
    console.log(result)
    // result return 42
}).catch(err => {
    // if name doesn't exist in client context
    // handle error 
});

callServer(name, args?)

call a procedure register in server context.

parameters
  • name string - The unique identifier, relative to the current context, of the procedure.
  • args Optional arguments to pass to the procedure.
Exemples

server

rpc.register('myevent', () => '42');

browser

//in server file
rpc.callServer('myevent').then((result) => {
    console.log(result)
    // result return 42
}).catch(err => {
    // if name doesn't exist in server context
    // handle error 
});

Changelog

23/10/2019 : add second argument to register in server that contain player and procedure name

rpc.register('myprocedure', (args, info) => {
    console.log(info);
    // info return { player: Player {}, name: 'myprocedure'}
});

About

Asynchronous Remote Procedure Call (altv-rpc)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages