# websockets.js and agentLogic ## What is websockets.js used for? The websockets.js file is in charge of creating a Websocket server and handling the connection of clients. It includes functions for handling data that is being passed between the client and the server. ### ws WebSocket is a technology that provides a two-way communication between the front-end and back-end. The ws npm module provides easy server side implementation, but requires the front-end native Websocket object to achieve full two-way communication. The ws implementation inside websockets.js for handling new connections, incoming messages, and closing connections: ```javascript= wss.on('connection', (ws, req) => { console.log('New Websocket Connection') // Getting the user data from the cookie try { const cookies = cookie.parse(req.headers.cookie) const userCookie = cookieParser.signedCookie(cookies['user']) userCookieParsed = JSON.parse(userCookie.substring(2)) } catch (error) {} ws.on('message', (message) => { try { const parsedMessage = JSON.parse(message) console.log('New Websocket Message:', parsedMessage) messageHandler( ws, parsedMessage.context, parsedMessage.type, parsedMessage.data, ) } catch (error) { console.error(error) } }) ws.on('close', (code, reason) => { console.log('Websocket Connection Closed', code, reason) }) }) ``` ### Function sendMessageToAll This function will first map through all the Websocket clients to make sure the connection is open. If the connection is open, a message is sent to the client. The sending message accepts a "context" and a "type" parameter. These parameters are used for defining how the client should handle the message and its data. The sendMessageToAll Function: ```javascript= // Send a message to all connected clients const sendMessageToAll = (context, type, data = {}) => { try { console.log(`Sending Message to all websocket clients of type: ${type}`) wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { console.log('Sending Message to Client') client.send(JSON.stringify({context, type, data})) } else { console.log('Client Not Ready') } }) } catch (error) { console.error('Error Sending Message to All Clients') throw error } } ``` ### Function sendMessage The sendMessage function is used inside of the messageHandler function for sending messages back to the connected Websocket client. Arguments can be passed using the "context" and "type" parameters. These parameters are used for defining how the client should handle the message and its data. The sendMessage function: ```javascript= // Send an outbound message to a websocket client const sendMessage = (ws, context, type, data = {}) => { console.log(`Sending Message to websocket client of type: ${type}`) try { ws.send(JSON.stringify({context, type, data})) } catch (error) { console.error(error) throw error } } ``` ### Function sendErrorMessage The sendErrorMessage function is used for calling the sendMessage function inside of websockets.js. It passes "ERROR" for the context and "SERVER_ERROR" as the type, for helping the client handle server errors. ```javascript= // Send an Error Message to a websocket client const sendErrorMessage = (ws, errorCode, errorReason) => { try { console.log('Sending Error Message') sendMessage(ws, 'ERROR', 'SERVER_ERROR', {errorCode, errorReason}) } catch (error) { console.error('Error Sending Error Message to Client') console.error(error) } } ``` ### Function messageHandler The messageHandler acts as a mediator for the websocket messages. It uses “context” and “type” in switch statements to determine the functionality of the request and to decide where the data and message should be sent. ## What is agentLogic used for? The agentLogic folder is used for storing all the controller files. The controller files contain functions for handling requests, which often interact with the adminAPI, ORM, or other controller functions. ### contacts.js Contains controller functions for interfacing with the Connections, Contacts, and ContactsCompiled ORMs; and handling the received data. ### credDefs.js Contains controller functions for calling AdminAPI functions to create or fetch Credential Definitions and Credential Definition IDs. ### credentials.js Contains a controller function for running multiple credential data validations. If all validations succeed then an AdminAPI function is called for auto issuing the credential. The file also contains functions for interacting with the Credentials ORM and handling the database data. ### demographics.js Contains controller functions for interacting with the Demographics and ContactsCompiled ORM's, and handling the received database data. ### dids.js Contains controller functions that call AdminAPI functions for setting, fetching, and creating public DIDs. ### emailVerification.js Contains a controller function for storing token information inside of the EmailVerification ORM and for sending a verification email via the NodeMailer module setup. The file also contains functions that provide some additional interaction with the EmailVerification ORM, which help with validating the stored token in order to issue a Validated_Email Credential. Token creation using jwt (JSON Web Token): ```javascript= const token = jwt.sign({email: email}, process.env.JWT_SECRET, { expiresIn: '24h', }) ``` Token being stored via EmailVerifications ORM: ```javascript= var emailVerification = { email: email, token: token, } EmailVerifications.create(emailVerification) ``` Sending verification email via NodeMailer: ```javascript= await NodeMailer.sendMail({ from: currentSMTP.dataValues.value.auth.user, to: email, subject: `${organization} Verify Your Email`, html: emailVerificationText, }) ``` ### images.js Contains controller functions for interacting with the Images ORM and handling the image data. ### invitations.js Contains controller functions that utilize the Connections AdminAPI and the Connections ORM to create an invitation that an agent can connect to. The functionality for accepting invitations is also included. ### ledger.js Contains controller functions that utilize the Ledger AdminAPI to fetch and accept the Transaction Author Agreement from the ledger. ### passport.js Contains controller functions for interacting with the Passports and ContactsCompiled ORM's, in order to Create or Update a Passport. ### presentations.js Contains controller functions for calling to the AdminAPI to request presentation of credentials, and functions for helping to verify the credential after it's been presented. Once verified, the credential is sent to the client. Requesting presentation of the Validated_Email credential: ```javascript= AdminAPI.Presentations.requestPresentation( connectionID, ['address'], 'TaDe8aSZMxoEU4GZDm9AKK:2:Validated_Email:1.0', 'Requesting Presentation', false, ) ``` ### roles.js Contains controller functions for interacting with the Users ORM, in order to create or read a Role. ### schemas.js Contains a controller function for fetching a schema by interacting with the AdminAPI. ### settings.js Contains controller functions for interfacing with the Settings ORM. The functions include updating or reading the Theme, SMTP, and Organization. ### users.js Contains controller functions for managing new and existing users. Some of this management includes interfacing with the User ORM, sending emails for validation purposes, and conditional programming to help authenticate users. Interfacing with the User ORM involves creating, reading, updating, and deleting from the data. The NodeMailer module is used for sending emails. The bcryptjs module helps with authentication.