Backend

From Rsewiki

Backend

Introduction

The backend of the Robot Dashboard is responsible for managing communication between the frontend and the Robobot. It is developed using Node.js and Express, with MQTT handling communication with the robot and Socket.IO providing real-time updates to the frontend.

The backend acts as an intermediary between the robot and the web application, processing incoming robot data and forwarding relevant information to the dashboard.

The backend consists of several files and components, each serving a specific purpose. Some files can be modified to customize the dashboard's functionality, while others contain core functionality and should generally remain unchanged to ensure the system operates correctly.

This section provides an overview of the backend architecture, its file structure, and guidelines for configuring and modifying the system.

Backend Architecutre

The backend of the Robot Dashboard is built using Node.js and Express and acts as a communication layer between the Robobot and the frontend application.

The architecture consists of three main components:

  1. Express: Handles HTTP requests, including user authentication and session management.
  2. MQTT: Enables communication between the backend and the Tuype robot through a publish-subscribe messaging system.
  3. Socket.IO: Establishes real-time, bidirectional communication between the backend and frontend, allowing robot data to be displayed without manually refreshing the page.

Communication Flow

The backend acts as an intermediary between the frontend and Robobot.

  1. The Robobot publishes data to specific MQTT topics through an MQTT broker.
  2. The backend subscribes to relevant topics and receives incoming robot data.
  3. The backend processes the received messages and forwards relevant information to the frontend using Socket.IO.
  4. The frontend receives these updates and displays them in the corresponding dashboard widgets.

For interactions that require sending information or commands to the robot, the communication can also work in the opposite direction, with the backend publishing messages to the appropriate MQTT topics.

Model-View-Controller

The Robot Dashboard was designed using the Model–View–Controller (MVC) architectural pattern to separate the application's user interface, application logic, and communication handling.

The architecture consists of three main components:

  • Model: Represents the backend logic and is responsible for processing data and managing the application's functionality.
  • View: Represents the frontend user interface, where users can interact with the dashboard and visualize real-time robot data.
  • Controller: Acts as an intermediary between the Model, View, and Tuype robot. It handles communication through REST APIs, Socket.IO, and MQTT, as well as session management.

When a user interacts with the dashboard, the input is sent from the View to the Controller. The Controller processes the request and communicates with the Model when application logic or data processing is required.

For robot-related interactions, the Controller communicates with the Tuype robot through MQTT. The robot publishes data to MQTT topics, which the backend subscribes to and forwards to the frontend through Socket.IO.

The Controller is also responsible for sending updates back to the View, allowing the dashboard to display real-time information without requiring the user to refresh the page.

Authentication and Session Management

The backend implements a password-based authentication system to restrict access to the Robot Dashboard.

User authentication is handled through Express, where the provided password is verified against a stored password hash using bcrypt. Upon successful authentication, a session is established using express-session

The session is maintained through a cookie, allowing the backend to recognize authenticated users across subsequent requests without requiring them to log in repeatedly.

The current implementation uses a single shared password rather than individual user accounts.

Backend Codebase Overview

The backend source code of the Robot Dashboard is located in the back-end-node-js/ directory, which serves as the root directory of the backend application.

The codebase is organized into several directories and files, each responsible for specific functionality, including API handling, MQTT communication, security, and real-time communication. This separation helps developers navigate the codebase, understand the responsibilities of individual modules, and identify relevant files for maintenance or further development.

The following sections provide an overview of the backend directory structure and the responsibilities of its main files and modules.

Configuration and Customization

The backend contains several configuration settings that allow developers to adapt the system to different requirements.

Some settings can be modified without affecting the underlying application logic, while others require changes across multiple files to maintain proper communication between the frontend, backend, and Robobot

This section outlines the available configuration options, their purposes, and the considerations developers should be aware of before making changes.

Environment Variables

The backend contains two .env files located in different directories, each serving a different purpose:

  • Root directory (back-end-node-js/.env): Contains the environment variables used by the main backend application, including AdminPassword and SessionSecret. These variables are used for authentication and session management.
  • Scripts directory (back-end-node-js/scripts/.env): Contains environment variables used specifically by scripts and test files within the scripts/ directory. These settings are separate from the main backend configuration.
Main Backend Environment Variables

The main backend .env file contains two environment variables:

  • AdminPassword: Stores the bcrypt-hashed administrator password. This value should only be modified when changing the dashboard's login password. The new password must first be hashed using bcrypt before replacing the existing value.
  • SessionSecret: Used to sign session cookies and protect their integrity. This value should generally remain unchanged, as modifying it may invalidate existing user sessions. It should only be changed when necessary, such as when rotating a compromised secret.

Both variables contain sensitive information and should not be publicly shared or included in version control.

When modifying environment variables, ensure that the correct .env file is updated, as changes to one file may not affect the other.

For instructions on modifying these settings, see Authentication and Session Configuration.

MQTT Configuration

Explain how to configure the MQTT broker, connection settings, and topics.

Service filles Configuration

The service files acts as the logic in the code and the ones that can and should be modifcied is tabService

Socket.IO Configuration

Explain real-time communication settings.

Authentication and Session Configuration

This section explains how to modify the authentication settings used by the Robot Dashboard.

The \security/\ directory contains the script responsible for handling authentication. This script should remain unchanged.

Changing the Administrator Password

The administrator password is stored as a bcrypt hash in the .env file located in the backend root directory.

To change the administrator password, follow these steps:

Step 1: Generate a New Password Hash
  1. Open back-end-node-js/scripts/generateHash.js.
  2. Locate the variable userPassword and replace its current value with the desired new password.
  3. Open a terminal and navigate to the scripts/ directory.
  4. Run the following command to generate a new bcrypt hash:
    node generateHash.js
  5. Copy the generated hash (the second output in the terminal).
Step 2: Update the Environment Variable
  1. Open the .env file located in the backend root directory (back-end-node-js/.env).
  2. Locate the AdminPassword variable.
  3. Replace its existing value with the newly generated bcrypt hash.
  4. Save the file and restart the backend application to apply the changes.
  5. To restart the backend application, go to the robot's terminal and type sudo systemctl restart webapp.service
Step 3: Verify the New Password
  1. Open back-end-node-js/scripts/testHash.js.
  2. Update the test password to match the newly chosen administrator password.
  3. Ensure that the test script compares the new password against the newly generated hash.
  4. Run the test script from the scripts/ directory:
    node testHash.js
  5. The script should return true if the password matches the hash, or false if they do not match.

Note: The administrator password must always be hashed using bcrypt before being stored in the .env file. Never store plaintext passwords in environment variables or commit passwords and hashes to version control.

Session Secret

The SessionSecret variable is used to sign session cookies. It should not be modified during normal configuration.

If the secret needs to be rotated, replace it with a new, securely generated random value. Changing the secret may invalidate existing sessions, requiring users to log in again.

If a new session needs to be generated, type from the root directory node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

Core files and Restrictions