---
# System prepended metadata

title: Websockets Message Handler

---

# Websockets Message Handler

## What does the Message Handler do?
The message handler 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.

Each case inside of the "context" switch statement contains an additional switch statement for handling "type". Each "type" case will include interacting with agentLogic. This interaction should relate to the "context" and its specific action should be defined using "type". 

Example code of using "context" and "type" inside the messageHandler:
```
// The 'INVITATIONS' is the context.
case 'INVITATIONS':
        switch (type) {
          case 'CREATE_SINGLE_USE':
            if (check(rules, userCookieParsed, 'invitations:create')) {
              var invitation
              if (data.workflow) {
                invitation = await Invitations.createPersistentSingleUseInvitation(
                  data.workflow,
                )
              } else {
                invitation = await Invitations.createSingleUseInvitation()
              }
              sendMessage(ws, 'INVITATIONS', 'INVITATION', {
                invitation_record: invitation,
              })
            } else {
              sendMessage(ws, 'INVITATIONS', 'INVITATIONS_ERROR', {
                error: 'ERROR: You are not authorized to create invitations.',
              })
            }
            break

```

If needed, the "type" case can call the sendMessage function that's located inside of the websocket.js file. This sendMessage function is used for sending messages back to the connected client. The sendMessage function accepts "context" and "type" arguments to help the connected client decide on how the received data should be handled.

Example of sendMessage code:
```
// 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
  }
}

//Calling sendMessage
sendMessage(ws, 'INVITATIONS', 'INVITATION', {invitation_record: invitation,})
```
 
## USERS context:
All calls are made to agentLogic/users.js controller file, where functions for retrieving and managing users' data are located.

### GET_ALL
Gets all users' data and sends a message back to the Websocket client with the data attached.

### GET
Gets user's data by userID and sends a message back to Websocket client with the data attached.

### GET_USER_BY_TOKEN
Gets user's data by userToken and sends a message back to Websocket client with the data attached.

### GET_USER_BY_EMAIL
Gets user's data by userEmail and sends a message back to Websocket client with the data attached.

### CREATE
Creates a new user and sends a success message back to Websocket client, as long as conditions are met.

### UPDATE
If conditions are met, this case will update an existing user's data and send a success message back to the Websocket client.

### PASSWORD_UPDATE
If conditions are met, this case will update an existing user's password and send a message back to the Websocket client with the updated user data.

### DELETE
If conditions are met, this case deletes an existing user and sends a success message back to the Websocket client.

### RESEND_CONFIRMATION
If conditions are met, this case will update an existing user's data and send an confirmation email. After the email is sent, a success message is sent back to the Websocket client.

## ROLES context:
All calls are made to agentLogic/roles.js controller file, where functions for retrieving and managing role data are located.

### GET_ALL
If conditions are met, this case will get all roles' data and send a message back to the Websocket client with the data attached.

### GET
Gets role data by roleID and sends a message back to Websocket client with data attached. 

## INVITATIONS context:
All calls are made to agentLogic/invitations.js controller file, where functions for retrieving and managing invitation data are located.

### CREATE_SINGLE_USE
If conditions are met, this case will create a single use invitation that supports connection reuse or it will create a single use invitation with no connection reuse support. When either one of the invitations have been created, a message is sent back to the Websocket client with the invitation data attached. 

### ACCEPT_INVITATION
If conditions are met, this case will use the invitation url to accept the invitation.

## CONTACTS context:
All calls are made to agentLogic/contacts.js controller file, where functions for retrieving and managing contacts' data are located.
### GET_ALL
If conditions are met, this case will get all contacts' data and send a message back to Websocket client with the data attached.

### GET
Gets contact data by contactID and sends a message back to Websocket client with the data attached.

## DEMOGRAPHICS context:
All calls are made to agentLogic/demographics.js controller file, where functions for retrieving and managing demographic data are located.

### UPDATE_OR_CREATE
If conditions are met, this case will create or update the demographics, along with getting a contact by contact ID. Once contact data is retrieved, a message is sent back to all Websocket clients with the data attached. 

## PASSPORTS context:
All calls are made to agentLogic/passports.js controller file, where functions for retrieving and managing passport data are located.

### UPDATE_OR_CREATE
This case will create or update the passports, along with getting a contact by contact ID. Once contact data is retrieved, a message is sent back to all Websocket clients with the data attached. 

## SETTINGS context:
All calls are made to agentLogic/settings.js controller file, where functions for retrieving and managing setting data are located.

### SET_THEME
If conditions are met, this case will update and get theme data. Once theme data is retrieved, two messages are sent back to the Websocket client. One message with the theme data, and the other a success message. 

### GET_THEME
Gets theme data and sends a message back to Websocket client with the data attached.

### SET_SMTP
If conditions are met, this case will update and get SMTP data. When complete, a success message is sent back to the Websocket client.

### SET_ORGANIZATION_NAME
If conditions are met, this case will update and get organization data. When complete, two messages are sent back to the Websocket client. One message with the organization data, and the other a success message. 

### GET_ORGANIZATION_NAME
Gets organization data and sends a message back to the Websocket client with the data attached.

## IMAGES context:
All calls are made to agentLogic/images.js controller file, where functions for retrieving and managing images data are located.

### SET_LOGO
If conditions are met, this case will update and get image data. When complete, two messages are sent back to the Websocket client. One message with the image data, and the other a success message. 

## CREDENTIALS context:
All calls are made to agentLogic/credentials.js controller file, where functions for retrieving and managing credential data are located.

### ISSUE_USING_SCHEMA
If conditions are met, this case will issue a credential.

### GET
Gets credential data by credential exchange ID and sends a message back to the Websocket client with the data attached.

### GET_ALL
Gets all credential data and sends a message back to the Websocket client with the data attached.





