ntfy

repository·main·Indexed 12 days ago

https://github.com/binwiederhier/ntfy

An HTTP-based pub-sub notification service that allows users to send push notifications to phones or desktops via PUT/POST requests. It can be used via the hosted ntfy.sh service or self-hosted. Features include a Go client for publishing and subscribing to topics, a server-side access control tool (ntfy access), and a migration utility (pgimport) for moving data from SQLite to PostgreSQL.

Tokens
92.6K
Snippets
254
Records
385
Agent score
97%

What's inside ntfy

  1. Overview of ntfy installation and configuration

    main

    The ntfy CLI allows you to publish messages, subscribe to topics, and self-host an ntfy server.

    Note: If you only want to send messages using the hosted ntfy.sh service, you do not need to install anything; you can use curl instead.

    General Installation Workflow:

    1. Install the binary, package, or Docker image.
    2. (Optional) Configure the server by editing /etc/ntfy/server.yml (Linux only).
    3. (Optional) Configure the client by editing:
      • ~/.config/ntfy/client.yml (Linux/macOS non-root user)
      • ~/Library/Application Support/ntfy/client.yml (macOS non-root user)
      • /etc/ntfy/client.yml (root user)

    Common Commands:

    • ntfy serve: Starts the ntfy server.
    • ntfy publish: Sends a message.
    • ntfy subscribe: Subscribes to a topic.
  2. Overview of ntfy.sh

    main

    ntfy (pronounced "notify") is a simple HTTP-based pub-sub notification service. It allows you to send push notifications to your phone or desktop via scripts from any computer without requiring a sign-up or fees.

    Key features include:

    • Pub-Sub Pattern: Send notifications via PUT or POST requests to specific topics.
    • No Sign-up Required: Use the free version at ntfy.sh immediately.
    • Self-Hostable: The project is open source, allowing you to run your own instance.
    • Mobile Support: Official open-source apps are available for Android (Google Play, F-Droid) and iOS (App Store).
  3. Understand the ntfy service model

    main

    ntfy is an HTTP-based pub-sub notification service that allows sending push notifications to phones or desktops via a REST API.

    The service consists of:

    • The ntfy.sh hosted server
    • The ntfy web application
    • Mobile applications (Android and iOS)
    • The ntfy command-line interface (CLI)

    Note on Hosting: The server software and mobile apps are open source. While ntfy.sh provides a hosted service, users requiring guaranteed uptime, specific service level agreements (SLAs), or full control over infrastructure and sensitive data are encouraged to self-host.

  4. What is template/gotext and why is it used?

    main

    The template/gotext package is a vendored and patched version of Go's standard library text/template.

    It exists to prevent CPU Denial of Service (DoS) attacks via user-supplied message templates. Standard Go templates cannot be interrupted mid-execution, meaning a malicious template with complex loops (e.g., {{range}}) could consume CPU indefinitely.

    template/gotext solves this by adding context-aware execution via ExecuteContext. This allows ntfy to set a timeout or deadline on template rendering; if the template takes too long or the request is canceled, the execution is aborted immediately.

  5. Understand the ntfy codebase structure

    main

    The ntfy project consists of three main components baked into a single binary:

    • Main server/client: Written in Go. The entrypoint is main.go and core logic resides in server/server.go. It uses go-sqlite3, which requires CGO_ENABLED=1 and a C compiler.
    • Documentation: Generated using MkDocs and Material for MkDocs (Python-based). Sources are in the docs/ directory.
    • Web app: A React application using MUI and Vite. Sources are in the web/ directory.

    During the build process, the generated web app is copied to server/site and documentation to server/docs to be embedded into the final binary.

  6. Android App: Instant Delivery vs. Firebase (FCM)

    main

    The Android app has two primary ways to receive notifications:

    1. Firebase Cloud Messaging (FCM): Used by the ntfy.sh server. This allows the app to receive notifications without maintaining a constant connection, resulting in minimal battery impact. This requires the FirebaseKeyFile to be configured on the server.
    2. Instant Delivery: An Android-only feature where the app maintains a constant connection to the server to listen for notifications. This provides immediate delivery but consumes more battery (approximately 0-1% per 17 hours of use).

    Note: If you self-host your own server or use the F-Droid version of the app (which does not support FCM), the app must maintain a constant connection, which uses more battery than the FCM-enabled version.

  7. Choose between Free and Paid plans

    main

    ntfy offers two main usage tiers:

    Free Tier

    • Can be used without creating an account or subscribing.
    • Subject to rate limits and other restrictions.
    • Provides access to premium features (e.g., reserved topics and advanced access control).
    • Billed in advance on a recurring monthly or annual basis.
    • Subscriptions automatically renew unless canceled via account settings in the web application.
    • Payment processing is handled by Stripe.
  8. Use message templating with JSON webhooks

    main

    ntfy supports Go templates to format JSON message bodies into human-friendly notification text and titles. This is particularly useful for services like GitHub, Grafana, or Alertmanager that emit JSON webhooks. Instead of writing a bridge program to parse the JSON, you can use templating to extract specific fields from the webhook payload directly into your notification.

    You can enable templating using the X-Template header (aliases: Template, Tpl), or the ?template= query parameter. There are three ways to use it:

    1. Pre-defined templates: Use built-in templates for common services (e.g., ?template=github).
    2. Custom template files: Use your own YAML files stored in the server's template directory (e.g., ?template=myapp).
    3. Inline templating: Enable parsing of the message, title, and/or priority fields as Go templates by setting the template value to yes or 1 (e.g., ?template=yes).
    # Example: Using a pre-defined github template via query parameter
    https://ntfy.sh/mytopic?template=github
  9. Data privacy and message caching

    main
    When using the ntfy.sh public server, topic names and IP addresses are recorded in logs for troubleshooting and rate limiting. Messages are cached for a duration defined in server.yml (default is 12h) to support message polling, client network disruptions, and service restarts. If you require absolute privacy for sensitive messages, it is recommended to self-host your own ntfy instance.
  10. Secure your topics

    main

    On the hosted ntfy.sh service, topic names are public.

    If you are using the service without access controls (the free tier), your topic name effectively functions as a password. You are responsible for choosing topic names that are not easily guessable to prevent unauthorized access to your messages. For more robust security via reserved topics and access control features, consider a paid subscription.

  11. Collect data from multiple machines using ntfy raw streams

    main

    You can use ntfy as a lightweight data aggregator. Multiple machines can POST data to a single topic, and a central collector can consume the raw stream using curl to append results to a file.

    Publisher (on remote machines): curl -d "<data>" ntfy.sh/<topic>

    Collector (central machine): Use curl -s ntfy.sh/<topic>/raw to read the stream.

    # collect-results.sh
    while read result; do
      [ -n "$result" ] && echo "$result" >> results.csv
    done < <(stdbuf -i0 -o0 curl -s ntfy.sh/results/raw)