Neutralinojs Documentation

repository·main·Indexed 42 days ago

https://github.com/neutralinojs/neutralinojs.github.io

Documentation for the Neutralinojs framework, featuring APIs for application control (app.exit, app.restartProcess), system clipboard management, and comprehensive computer hardware information including CPU, memory, disk, and network interface details. Includes guides on implementing custom native C++ APIs within the framework core.

Tokens
36.9K
Snippets
158
Records
178
Agent score
54%

What's inside Neutralinojs

  1. What is Neutralinojs?

    main
    Neutralinojs is a lightweight, portable framework for developing cross-platform desktop applications using standard web technologies: JavaScript, HTML, and CSS. Unlike Electron or NW.js, it does not bundle Chromium; instead, it utilizes the operating system's existing web browser library (such as gtk-webkit2 on Linux) and implements a WebSocket connection for native operations. This makes applications significantly smaller and less resource-intensive.
  2. Understand Neutralinojs build outputs

    main

    Running neu build generates platform-specific application binaries and a shared resource file in the dist directory.

    By default, the build produces two separate files per platform: the application binary and resources.neu. The resources.neu file contains all application resources. For a successful portable distribution, you must include both the specific binary and the resources.neu file for the target OS.

    Supported Build Outputs:

    FilenameOSCPU architectureType
    myapp-linux_x64Linuxx86_64Application binary
    myapp-linux_armhfLinuxarmhfApplication binary
    myapp-linux_arm64Linuxarm64Application binary
    myapp-mac_x64macOSx86_64Application binary (Intel)
    myapp-mac_universalmacOSx86_64 and arm64Application binary
    myapp-mac_arm64macOSarm64Application binary (M1 onward)
    myapp-win_x64Windowsx86_64Application binary
    resources.neuallN/AApplication resource file
    neu build
  3. Manage file streams with openFile and updateOpenedFile

    main

    For advanced file handling, use Neutralino.filesystem.openFile to create a readable file stream, which returns a file stream identifier (id). You can then use Neutralino.filesystem.updateOpenedFile to perform actions on that stream.

    Available actions for updateOpenedFile:

    • read: Reads a bytes segment (default 256 bytes unless specified in data.size).
    • readBinary: Binary version of read.
    • readAll: Reads until EOF.
    • readAllBinary: Binary version of readAll.
    • seek: Sets the file cursor position (requires data to contain the position).
    • close: Closes the stream.

    Note: Stream events like data and end are emitted via Neutralino.events.

    let fileId = await Neutralino.filesystem.openFile('./myFile.txt');
    
    // Listen for stream events
    Neutralino.events.on('openedFile', (evt) => {
      if(evt.detail.id == fileId) {
        if(evt.detail.action === 'data') console.log('Chunk:', evt.detail.data);
        if(evt.detail.action === 'end') console.log('EOF reached');
      }
    });
    
    // Seek to 10th byte and read 2 bytes
    await Neutralino.filesystem.updateOpenedFile(fileId, 'seek', 10);
    await Neutralino.filesystem.updateOpenedFile(fileId, 'read', 2);
  4. Choose a Neutralinojs execution mode

    main

    Neutralinojs applications can run in four distinct modes depending on your deployment requirements. The default mode is window.

    • window: Runs the application in a native window using the operating system's theme. Best for cross-platform desktop application development.
    • browser: Loads the application in the user's default web browser. This allows web applications to access the operating system layer via Neutralinojs while maintaining security controls.
    • cloud: Runs the Neutralinojs process as a background server. This mode allows you to expose your application to a public network or the internet, or use it as a lightweight message broker on bare-metal servers, VMs, or containers.
    • chrome: Runs the application as a Chrome/Chromium application, making the web app feel more like a native application.
  5. Connect an extension to the Neutralinojs server

    main

    When Neutralinojs spawns an extension, it sends a JSON object via standard input (stdin) containing connectivity details. The extension must use these details to establish a WebSocket connection to the Neutralinojs server.

    Connection Details (from stdin):

    • nlPort: The port of the Neutralinojs server.
    • nlToken: Access token for the native API.
    • nlConnectToken: Token required for WebSocket connection initialization.
    • nlExtensionId: The unique identifier for the extension.

    WebSocket URL Format: ws://localhost:{nlPort}?extensionId={nlExtensionId}&connectToken={nlConnectToken}

  6. How Native API calls work

    main

    Native API calls follow a WebSocket-based communication pattern between the JavaScript client library and the Neutralinojs server:

    1. Invocation: You call a JavaScript method (e.g., Neutralino.os.getEnv).
    2. Request: The client library sends a WebSocket message to the Neutralinojs server. The library uses a task pool and a unique UUID string to map server responses back to the original request.
    3. Execution: The Neutralinojs server executes the requested native operation on the operating system.
    4. Response: The server sends a WebSocket message back to the client with the result.
    5. Resolution: The client library resolves the original JavaScript Promise with the received data.
  7. How to develop a neu CLI plugin

    main

    To create a plugin, develop an npm package that exports an index.js file. The CLI uses this file as an interface to register new commands using a register function. The plugin uses a pattern similar to the commander package for defining command strings, options, and actions.

    // index.js
    module.exports = {
      command: 'commandname <action>',
      register: (command, modules) => {
        command.option('--option1 --option2')
         .action((action, command) => {
            // your logic goes here..
         });
      }
    }