Tag Archives: ntfy

Reading ntfy stream in vue3-ts page using axios

The following worked for me


onMounted(async () => { refreshMetricInterval = setInterval((_) => { fetchData() }, C.DEFAULT_POLL_INTERVAL_SEC * 1000) // Subscribe to ntfy. const ntfy = await axios.get('https://ntfy.sh/mytopic/json', { headers: { Authorization: 'Bearer yout_token', Accept: 'text/event-stream', }, responseType: 'stream', adapter: 'fetch', }); // This doesn't work. We are using fetch API and the below solutions work.s // // ``` // console.debug('axa1', ntfy.data) // ntfy.data.on('message', (data) => { // console.debug(444, data); // }); // ``` for await (const data of ntfy.data) { const json = JSON.parse(new TextDecoder().decode(data)); console.debug('Got line from ntfy', json); } })

self-hosted ntfy service with admin account

  • First, we need a script that setup admin account for us after starting the service. init.sh file that you should put inside ntfy/init.sh
    #!/bin/sh
    ntfy serve &
    sleep 2
    NTFY_PASSWORD=${NTFY_PASSWORD} ntfy user add --role=admin ${NTFY_USER}
    wait
    
  • Now the compose file compose.yaml or docker-compose.yaml file.
    services:
      ntfy:
        image: binwiederhier/ntfy
        container_name: ntfy
        entrypoint: [ "/bin/sh", "/var/lib/ntfy/init.sh" ]
        environment:
          - TZ=UTC    \# optional: set desired timezone
          - NTFY_BASE_URL=https://ntfy.dilawars.me
          - NTFY_AUTH_FILE=/var/lib/ntfy/auth.db
          - NTFY_CACHE_FILE=/var/lib/ntfy/cache.db
          - NTFY_AUTH_DEFAULT_ACCESS=deny-all
          - NTFY_ATTACHMENT_CACHE_DIR=/var/lib/ntfy/attachments
          - NTFY_ENABLE_LOGIN=true
          - NTFY_USER=admin
          - NTFY_PASSWORD=yoyodillusingh
        volumes:
          - /var/cache/ntfy:/var/cache/ntfy
          - /etc/ntfy:/etc/ntfy
          - ./ntfy/:/var/lib/ntfy
        ports:
          - 8001:80
        healthcheck: \# optional: remember to adapt the host:port to your environment
            test: ["CMD-SHELL", "wget -q --tries=1 http://localhost:80/v1/health -O - | grep -Eo '\"healthy\"\\s*:\\s*true' || exit 1"]
            interval: 60s
            timeout: 10s
            retries: 3
            start_period: 40s
        restart: unless-stopped