Notifo Documentation

repository·main·Indexed 21 days ago

https://github.com/notifo-io/notifo

A multi-channel notification service for collaboration tools, e-commerce, and news platforms. Notifo features a hierarchical topic-based subscription model and supports delivery via Email (Amazon SES), WebPush, SMS (MessageBird), MobilePush (Google Firebase), and WebSockets (SignalR). It includes a Node.js SDK (@notifo/notifo) for API interaction and a UI SDK for managing real-time connections and push notification subscriptions.

Tokens
8.8K
Snippets
33
Records
43
Agent score
75%

What's inside Notifo

  1. Understanding notification confirmation modes

    main

    Notifo supports different confirmation preferences to prevent spamming users. Notifications can be configured with the following modes:

    • None: No confirmation required.
    • Explicit: The user must explicitly confirm the notification. Once confirmed, the system stops sending it through other channels.
    • Seen: The system tracks if a user has seen the notification. Once marked as 'seen', redundant notifications are avoided.

    Only unconfirmed notifications are sent through active channels, and you can configure delays for sending these notifications based on these modes.

  2. How Notifo's topic-based notification system works

    main

    Notifo uses a hierarchical topic-based subscription model to route notifications:

    1. Subscriptions: Users subscribe to specific topic paths (e.g., clothes/shoes/nike). Subscriptions can be for exact paths or parent paths, allowing users to receive notifications for all sub-topics.
    2. Events: The backend creates events using specific topic paths (e.g., clothes/shoes/nike/<model>).
    3. Matching: Notifo matches these events against user subscriptions. If a user is subscribed to a parent path, they receive notifications for all child paths under that hierarchy.
    4. Preferences: Users can define individual notification preferences (e.g., only Web or only Email) for specific paths or parent paths.
  3. Run Notifo on localhost using Docker Compose

    main

    Notifo requires HTTPS to function. When running on localhost, you must configure Caddy (the reverse proxy) to use a local certificate authority instead of Let's Encrypt.

    1. Configure Caddy: In your docker-compose.yml file, ensure the SITE_SETTINGS environment variable is set to "tls internal". If it is currently commented out, uncomment it:
    - SITE_SETTINGS="tls internal"
    1. Install the Root Certificate: Since the certificate is generated inside the Docker container, you must manually download and install it to your host machine's trusted root authorities store to avoid HTTPS errors in your browser.

    First, download the certificate from the running proxy container:

    docker cp docker-compose-notifo_proxy-1:/data/caddy/pki/authorities/local/root.crt .

    Then, install the downloaded root.crt into your operating system's trusted root authorities store. You may need to restart your browser (e.g., Chrome) for the changes to take effect.

  4. Install Notifo using Docker

    main

    You can run Notifo using Docker images available on Docker Hub. For a quick setup, you can use the provided docker-compose.yml file located in the deployment/docker compose/ directory of the repository.

    Docker images are hosted at: https://hub.docker.com/r/squidex/notifo.

    # Example using the provided docker-compose file
    docker compose -f deployment/docker\ compose/docker\ compose.yml up
  5. Configure TypeScript ESM projects for Notifo

    main

    The Notifo Node SDK is transpiled to CJS JavaScript for maximum compatibility. If your project uses TypeScript ESM, you must enable esModuleInterop in your tsconfig.json to ensure imports work correctly.

    {
      "compilerOptions": {
        "esModuleInterop": true,
        ...
      }
    }
  6. Install the @notifo/notifo library

    main

    Install the Notifo Node.js library using npm or yarn. This library provides access to the Notifo API from both Node.js and the browser environments. Requires TypeScript 4.5 or higher.

    npm install @notifo/notifo
    # or
    yarn add @notifo/notifo
  7. Configure Notifo using environment variables

    main

    Notifo is configured via an appsettings.json file. All settings in this file can be overridden using environment variables.

    To map a nested JSON configuration key to an environment variable, use double underscores (__) as a delimiter. For example, the connectionString inside the mongoDB object is mapped to MONGODB__CONNECTIONSTRING.

    // appsettings.json structure
    "mongoDB": {
        "connectionString": "mongodb://localhost"
    }
    
    // Corresponding environment variable
    export MONGODB__CONNECTIONSTRING="mongodb://localhost"
  8. How to manage the UI lifecycle with release()

    main

    When using the Notifo UI SDK, you should call UI.release(elementOrId) when the component or page containing the notification UI is unmounted. This prevents memory leaks and ensures that the DOM is cleared of any injected Notifo elements.

    If the provided elementOrId cannot be found, the function resolves silently without error.

  9. Connection types in the Notifo UI SDK

    main

    The Notifo UI SDK uses a Connection abstraction to manage real-time or periodic updates. Depending on your configuration, buildConnection returns a SafeConnection wrapping one of the following implementations:

    • SignalRConnection: Provides real-time updates via SignalR. It can be configured for socket-based communication.
    • PollingConnection: Provides updates by periodically fetching data from the server.
  10. Deploy Notifo using Docker Compose

    main

    Notifo can be deployed as a multi-container setup using Docker Compose. The deployment consists of three main services:

    1. notifo_notifo: The core Notifo application service.
    2. notifo_mongo: A MongoDB instance for data persistence.
    3. notifo_proxy: A Caddy-based proxy for handling HTTP/HTTPS traffic and SSL.

    All services communicate over an internal bridge network named internal.

    version: "3.5"
    services:
      notifo_mongo:
        image: mongo:5
        volumes:
          - /etc/notifo/mongo/db:/data/db
        networks:
          - internal
        restart: unless-stopped
    
      notifo_notifo:
        image: "squidex/notifo:1"
        environment:
          - URLS__BASEURL=https://${NOTIFO_DOMAIN}
          - STORAGE__MONGODB__CONNECTIONSTRING=mongodb://notifo_mongo
          - IDENTITY__GOOGLECLIENT=${NOTIFO_GOOGLECLIENT}
          - IDENTITY__GOOGLESECRET=${NOTIFO_GOOGLESECRET}
          - IDENTITY__GITHUBCLIENT=${NOTIFO_GITHUBCLIENT}
          - IDENTITY__GITHUBSECRET=${NOTIFO_GITHUBSECRET}
          - IDENTITY__MICROSOFTCLIENT=${NOTIFO_MICROSOFTCLIENT}
          - IDENTITY__MICROSOFTSECRET=${NOTIFO_MICROSOFTSECRET}
          - ASPNETCORE_URLS=http://+:5000
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:5000/healthz"]
          start_period: 60s
        depends_on:
          - notifo_mongo
        volumes:
          - /etc/notifo/assets:/app/Assets
        networks:
          - internal
        restart: unless-stopped
    
      notifo_proxy:
        image: squidex/caddy-proxy:2.7.6
        ports:
          - "80:80"
          - "443:443"
        environment:
          - SITE_ADDRESS=${NOTIFO_DOMAIN}
          - SITE_SERVER="notifo_notifo:5000"
        volumes:
          - /etc/notifo/caddy/data:/data
          - /etc/notifo/caddy/config:/config
        depends_on:
          - notifo_notifo
        networks:
          - internal
        restart: unless-stopped
    
    networks:
      internal:
        driver: bridge
  11. Integrate the Notifo UI SDK

    main

    The UI module provides the primary entrypoints for integrating Notifo notification overlays, topic management, and Web Push subscription prompts into your web application. You can target a specific DOM element by providing either its id (as a string) or the HTMLElement itself.

    Core Functions

    • setupNotifications(elementOrId, opts, config): Renders the main notification overlay.
    • setupTopic(elementOrId, topic, opts, config): Renders a UI component for a specific topic.
    • askForWebPush(config, options): Triggers a Web Push subscription prompt (e.g., a permission dialog).
    • release(elementOrId): Cleans up and removes the Notifo UI from the specified element.

    Configuration and Styles

    If config.styleUrl is provided, the SDK will automatically load the CSS from that URL before rendering the UI components.

    import { UI } from '@notifo/ui'; // Assuming package name
    
    // 1. Setup Notifications
    await UI.setupNotifications('#notification-container', {
      // NotificationsOptions
    }, {
      styleUrl: 'https://cdn.example.com/notifo.css',
      // SDKConfig
    });
    
    // 2. Ask for Web Push permission
    const granted = await UI.askForWebPush(config);
    if (granted) {
      console.log('User allowed notifications');
    }
    
    // 3. Clean up
    UI.release('#notification-container');
  12. Run the local test suite with Docker Compose

    main

    The tools/TestSuite/local/docker-compose.yml file defines a local development environment for testing notifications. It spins up two utility services: webhookcatcher for inspecting outgoing webhooks and mailcatcher for inspecting outgoing emails. Both services run on a shared internal bridge network.

    docker-compose up