POC Socket Io

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

First POC for an alternative method to web socket with front-end

Work item: Chargeback Team Storys Backlog - Boards (azure.com)

Problem:

“As we still do not have the confirmation that we will use the web socket to communicate with the
frontend, we need to think about an alternative that meets the need to return the updated data to
the frontend.

Raising possible alternatives.

One idea would be to hold the request that the frontend makes and only return as soon as all the
queues have been processed by the backend and updated in the database.”

Technology used for this POC:

 NodeJs;
 Lib socket.io - https://www.npmjs.com/package/socket.io;
 Lib axios - https://www.npmjs.com/package/axios;

Method:

Three APIs were created. The first (Producer) is the service that will make the request for processing
a front-end call. The second (Consumer) is the service that will respond to the first API instantly and
later process the request asynchronously notifying the socket server. Finally, the server (Server) uses
the socket.io library to create stable communication between the APIs.
Example code
Server:

The server receives a communication in a context (‘message’) and then emit this message to all
connections throw an id context (variable ‘id’ in this case) with the exact same message received.

Consumer:
The consumer receives a request from the producer and then simulate a processing delay changing
the payload from anything to ‘processed’.

Producer:
The Producer receive a request from front-end and then send it to consumer to processes and so
wait receive a response from server and output to front-end.

Link to files: https://santandernet.sharepoint.com/sites/SMPS-BR-Team/Shared%20Documents/


Chargeback/New%20Architecture/POCs/poc-socket-io.rar

Conclusion:

 You can expect a request to be processed asynchronously and wait for a response via
socket.io.
 The id created to wait a request should be unique, because every API connected to server
will listen that message if connected to same id context.
 You should remove the listener after receive the message to keep a pattern and avoid stay
listeners opens. (ex. “socket.off(context)”).

You might also like