payment-microservice-client
Package to create a socket interface with redis ready to connect to a microservice using payment-microservice package.
Last updated a month ago by alber0905 .
ISC · Original npm · Tarball · package.json
$ cnpm install payment-microservice-client 
SYNC missed versions from official npm registry.

Payment Microservice Client

Description

Application side package for payment-microservice.

This package is meant to be a wrapper for applications using the payment-microservice package with redis.

Usage

Import the package main method:

import pmClient from 'payment-microservice-client';

This method will receive either the express server and the port in which it is running to start a socket server and client on it, or if you already have a socket server you can pass it to the function and it will use it.

If both the socket server and the express server are passed, the socket server will be used and the express server will be ignored.

Returns the socketServer.

Parameters

Parameter Type Description Optional
httpServer http.Server Express server Yes. Only needed if no socketServer is passed.
httpPort number Por in which the socket client will run. Should be the same as the express server port. No.
socketServer io.Server Socket server which will be used to set up the connection. Yes. Only needed if no httpServer and httpPort are passed
redisPort number Port in which the redis server is running . No
socketPath string Path in which the socket server will be running. Defaults to /ws Yes. Only needed if no socketServer is passed.
redisHost string Path or IP for the redis server No
clientName string Application name for this client. Used for logging. No
connectFn function. (socket: io.Socket) => void) Function which will be executed on the on connection event of the socket server. Yes

Example:


const app = express();
  app.use(cors());
  const server = createServer(app);
  server.listen(3005);
  pmClient({
    httpServer: server,
    httpPort: 3005,
    socketPath: '/ws',
    redisHost: '127.0.0.1',
    clientName: 'TestClient',
    connectFn: socketConnect,
  });

  const socketConnect = (socket: Socket): void => {...}

This method will create a socket server (if not passed) with a redisAdapter, and a socket client.

The socket client will connect to its own server, which will push it into a room with name hostName().

This socket client will listen to the alive event emmited by the payment-microservice package. This event will receive an object with a micro service name and a room name, which will be pushed into an array.

Socket interface

This package exposes a socket interface to communicate with the micro services.

To do this we will use the executeToRoom method.

const executeToRoom = <ReturnType>(
  name: string,
  params?: object,
  microName?: string,
): Promise<ReturnType> => {...}

This method will make a synchronous call to a micro service socket event. It receives the name of the event, the params for the event method and the micro service name.

It will take the most recent room in the array with the micro service name passed and will execute the event, which has been set with a methodWrapper from the payment-microservice package. When the micro service method ends its execution it will emit an event with an id supplied by executeToRoom, which will be listening for this event and resolve the promise.

If our application is only communicating with one micro service, we don't need to use the microName param, as it will take the most recent room from the array.

Example:

import Socket from 'payment-microservice-client/Socket';
const keys = await Socket.executeToRoom<string>('getKeys', inData, 'PayPal');

Current Tags

  • 1.0.13                                ...           latest (a month ago)

21 Versions

  • 1.0.13                                ...           a month ago
  • 1.0.12                                ...           6 months ago
  • 1.0.11                                ...           6 months ago
  • 1.0.10                                ...           6 months ago
  • 1.0.9                                ...           6 months ago
  • 1.0.8                                ...           6 months ago
  • 1.0.7                                ...           6 months ago
  • 1.0.6                                ...           6 months ago
  • 1.0.5                                ...           6 months ago
  • 1.0.4                                ...           8 months ago
  • 1.0.3                                ...           8 months ago
  • 1.0.2                                ...           9 months ago
  • 1.0.1                                ...           9 months ago
  • 1.0.0                                ...           10 months ago
  • 0.0.7                                ...           10 months ago
  • 0.0.6                                ...           10 months ago
  • 0.0.5                                ...           10 months ago
  • 0.0.4                                ...           10 months ago
  • 0.0.3                                ...           10 months ago
  • 0.0.2                                ...           10 months ago
  • 0.0.1                                ...           10 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 30
Dependencies (14)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |