npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@das3mical/adonis-mercure

v1.0.12

Published

A simple AdonisJS provider to interact with Mercure hub

Downloads

78

Readme

@das3mical/adonis-mercure

@das3mical/adonis-mercure is a Mercure client for AdonisJS, enabling the use of Server-Sent Events (SSE) to push real-time updates to your clients over HTTP.

Table of Contents

Getting Started

Prerequisites

To use this package, you must have a Mercure Hub instance running. The Mercure Hub is responsible for broadcasting the SSEs to clients.

Installation

This package is available on the npm registry. Install it using the following command:

npm install @das3mical/adonis-mercure

Next, configure the package by running the configuration command:

node ace configure @das3mical/adonis-mercure

Your package is now configured.

Configuration

Before using the package, you'll need to set up the configuration. The configuration is stored in the config/mercure.ts file.

Here are the key configuration options:

  • endpoint: The endpoint of the Mercure Hub.
  • adminToken: The JWT created to authenticate as an admin of the Mercure Hub.
  • jwt.alg: The algorithm used to sign the JWT. This should match the configuration of your Mercure Hub.
  • jwt-secret: The secret key used to sign the JWT. This must correspond with your Mercure Hub's configuration.

Admin Token Example

The `adminToken` must be generated using the same `alg` and `secret` as your Mercure Hub, with the following payload:

{
  "mercure": {
    "publish": ["*"]
  }
}

For example, given the secret `ChangeMe` and the algorithm `HS256`, the JWT would be:

eyJhbGciOiJIUzI1NiJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.mx2ROlYlE1rp7udoDy-WCdnpLdPuKWzDxoBJXGMK4OE

Usage

This package provides access to two main functions:

  1. generate: Used to generate a JWT token for client authentication.
  2. send: Used to publish updates to the Mercure Hub.

1. Generating JWT Tokens

The `generate` function creates a JWT token for a given payload, which can be used for subscribing to private topics.

import mercure from '@das3mical/adonis-mercure'

const token = mercure.generate({
  subscribe: ['your-topic'], // example: subscribe: ['/chat/1'],
})

2. Sending Updates

The `send` function allows you to send data to one or more topics. You can also mark events as private if needed.

The `send` method accepts three arguments:

  • topic: The topic to which the update will be published.
  • data: The data to be published.
  • isPrivate: Indicates whether the update should be private.

For more details on the `topic` and `data` arguments, please refer to the Mercure documentation.

import mercure from '@das3mical/adonis-mercure'

mercure.send('your-topic', {
  message: 'Hello, World!',
})

To send private events, set the `isPrivate` flag to `true`:

import mercure from '@das3mical/adonis-mercure'

mercure.send(
  'your-topic',
  {
    message: 'Private event',
  },
  true
)