UNPKG

@wbg-mde/server

Version:

MDE communication channel with Socket IO and express.

103 lines (85 loc) 3.71 kB
# Metadata Editor Server Module Server module created to manage the bi-directional async requests in the MDE application. This module will be acted as a middle layer to communicate to the repository and r-factory requests. ### SocketIO module This module makes the application bi-directional communication between web clients and servers, Socket IO has two parts: Server side code and client-side code. This package contains the server side codes of MDE application. ### Working methodology The starting point of the application is the server.ts file, this is mentioned in the package.json. This file contains a method called `start()`, this method will be created an express server instance on a provided PORT. This method will be called from the main application under "electron.server" module. PORT and other related parameters will be provided as options from the main application. ### To start server press F5 to start server ##### Main socket module This is the starting point to start the socket io server. Once the server is started it will create an instance of the "MainSocket" class and calls the `init()` method. This method will be created the server side socket instance and start the connection. ``` public init(http: any) { const io = require('socket.io')(http); // #. Track connections io.on('connection', (socket) => { // #. Assign active socket this.socket = socket; // #. Track disconnect this.socket.on('disconnect', () => { console.log('disconnected'); }); this.initAll(); }); } ``` ##### "initAll()" - method Split the requests as seperate classes for easy handling. This method will create an instance of each classes after the socket connection established. Example: Create a class to get the request for resequencing the variables ``` export class ResequenceSocket { private socket: any; constructor(activeSocket: any) { this.socket = activeSocket; } /* * Init resequence - Request will recieved here ************************************************/ public init() { this.socket.on('resequence-req', (params: string) => { this.resequence(JSON.parse(params)); }); } private resequence(params: any) { rfactory.resequence.resequence( params.variables, params.filePath, params.projectId, params.rPath, params.language, (response: any) => { //#. Send responce to client - side this.socket.emit('resequence-res', { response, projectId: params.projectId, fileId: params.fileId }) } ); } } ``` this class must be registered in the "ModuleSocket" class. ModuleSocket class will be crate the instance of the resequence class and start the socket listening by using 'resequence-req' key. ``` import { ResequenceSocket } from './modules'; export class ModuleSocket { constructor() { } public initAll(socket: any) { // #. Start resequence let resq = new ResequenceSocket(socket); resq.init(); } } ``` Once the socket established the connection, the "initAll" method from ModuleSocket will be called. Use `emit()` method to send response to the client-side. In client Socket IO application expect the response key method to recieve the response. ### Environment - Node 10.15.3 - Express 4.16.4 - socket.io 2.2.0 - typescript 3.3.4 ### Contributors - Anoop Xavier (anoopx@panapps.co) - Navin VI (navin.v.i@panapps.co) - Libin Thomas (libint@panapps.co) - Ghanashyam C Kartha (ghanashyam.c.k@panapps.co) License ---- MIT