Skip to content

practio/rpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@practio/rpc

Remote Procedure Call (RPC) over AMQP.

Install

$ npm install @practio/rpc

Usage

Client

import { createRpcClient } from '@practio/rpc/client';

main();

async function main () {
  const rpcClient = await createRpcClient({
    url: 'amqp://localhost'
  });

  const eventIds = await rpcClient.call('getEventIds', null, 'server');

  for (const eventId of eventIds) {
    const event = await rpcClient.call('findEvent', { eventId }, 'server');

    console.info(eventId, event);
  }

  await rpcClient.dispose();
}

Server

import { createRpcServer } from '@practio/rpc/server';
import { v4 } from 'uuid';

main();

async function main () {
  const events = new Array(100).fill(undefined).map(createRandomEvent);
  
  await createRpcServer({
    namespace: 'server',
    procedures: {
      findEvent,
      getEventIds
    },
    url: 'amqp://localhost'
  });

  async function findEvent({ eventId }) {
    return events.find(event => event.id === eventId);
  }

  async function getEventIds() {
    return events.map(({ id }) => id);
  }
}

function createRandomEvent() {
  return {
    id: v4(),
    ts: new Date()
  }
}

About

Remote Procedure Call (RPC) over AMQP.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published