payment-microservice-client
v2.1.0
Published
Package to create a socket interface with redis ready to connect to a microservice using payment-microservice package.
Downloads
77
Readme
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');