NodeJS Integration

NodeJS Integration

This guide provides instructions for integrating the FleetShare SDK into a NodeJS application. The SDK uses asynchronous functions to run concurrently with your main application logic. NodeJS is a JavaScript runtime built on Chrome’s V8 JavaScript engine, widely used for server-side and networking applications.

Requirements

Before you begin, ensure you have the following dependencies in your project:

  • NodeJS (version 12.0 or higher recommended)
  • npm (Node Package Manager)

Installation

Place the FleetShareSDK.js file in your project directory. It will be provided by the FleetShare team.

Integration Steps

1. Import the FleetShare SDK

Import the FleetShare SDK at the beginning of your JavaScript file:

const FleetShareSDK = require('./FleetShareSDK');

2. Define Your Async Function

Create an asynchronous function where you will start the FleetShare SDK and run your main program logic.

3. Start the FleetShare SDK

Invoke the FleetShare SDK within your async function. Use the provided apiKey and, if necessary, additional parameters like silent and deviceName.

Parameters:

  • apiKey: Your API key from the FleetShare SDK program.
  • silent: (Optional) If set to true, the SDK runs silently without outputting logs.
  • deviceName: (Optional) A unique name for the device running the SDK.

4. Implement Main Program Logic

Include your main program logic within the async function. The FleetShare SDK will continue to share bandwidth in the background while your program runs.

Example Implementation

Here’s an example of integrating the FleetShare SDK into a NodeJS application:

const FleetShareSDK = require('./FleetShareSDK');

(async () => {
    try {
        // Start FleetShare SDK in the background
        FleetShareSDK.startFleetShareSDK('YOUR API KEY HERE', false, 'your-device-name')
            .catch(error => console.error('Error in FleetShare SDK:', error));
        
        // Your main program logic goes here
        console.log('Main program is running...');
        
        // Simulate some work in the main program
        for (let i = 0; i < 10; i++) {
            console.log(`Main program iteration ${i + 1}`);
            await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second
        }
        
    } catch (error) {
        console.error('Error in main program:', error);
    }
})();

Best Practices

  1. Error Handling: Implement robust error handling within your async functions to manage potential issues.
  2. User Consent: Make sure to obtain user consent before initiating the bandwidth sharing process.
  3. Updating the SDK: Stay updated with new SDK versions and implement mechanisms to handle SDK updates gracefully, as older versions may become deprecated.
  4. Environment Variables: Consider using environment variables to store sensitive information like API keys, rather than hardcoding them in your application.

Ensure you follow these best practices to maintain a secure and user-friendly application.

Troubleshooting

If you encounter any issues while integrating or using the FleetShare SDK, consider the following:

  1. Ensure you’re using the latest version of the SDK.
  2. Check that all required dependencies are correctly installed in your project.
  3. Verify that your API key is correct and active.
  4. If you’re experiencing connection issues, check your network connectivity.

For further assistance, please contact the FleetShare support team.