Backend: Difference between revisions

From Rsewiki
Line 92: Line 92:
Both variables contain sensitive information and should not be publicly shared or included in version control.
Both variables contain sensitive information and should not be publicly shared or included in version control.


Please read more on: [[#Authentication and Session Configuration|Authentication and Session Configuration]]
For instructions on modifying these settings, see [[#Authentication and Session Configuration|Authentication and Session Configuration]].


====  MQTT Configuration ====  
====  MQTT Configuration ====  

Revision as of 10:48, 13 October 2026

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 .env file contains two environment variables: AdminPassword and SessionSecret. These variables are used for authentication and session management.

  • AdminPassword: Stores the hashed administrator password. This value should only be modified when changing the dashboard's login password. When updating it, 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.

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

MQTT Configuration

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

API Configuration

Explain configurable API settings and endpoints.

Socket.IO Configuration

Explain real-time communication settings.

Authentication and Session Configuration

Explain which authentication and session settings can be modified.

Authentication and Session Configuration

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

Changing the Administrator Password

The administrator password is stored as a bcrypt hash in the .env file.

To change the password:

  1. Generate a new bcrypt hash using the desired password.
  2. Open the .env file located in the back-end-node-js/ directory.
  3. Replace the existing AdminPassword value with the newly generated hash.
  4. Save the file and restart the backend server for the changes to take effect.
  5. Verify that the new password works by logging into the dashboard.

Note: The password must be hashed using bcrypt. Do not store the password in plaintext.

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.

Core files and Restrictions

Running the backend