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

@setten/mercure

v0.1.1

Published

Mercure Client for AdonisJS

Downloads

228

Readme

@setten/mercure is a Mercure client for AdonisJS.

Mercure allows you to use Server Sent Events to push data to your clients using Http.

Note

You must have a Mercure Hub instance running.


Getting Started

This package is available in the npm registry.

npm install @setten/mercure

Next, configure the package by running the following command.

node ace configure @setten/mercure

and... Voilà!

Configuration

You have to configure the package before you can use it. The configuration is stored in the config/mercure.ts file.

  • 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 - should correlate to Mercure Hub configuration.
  • jwt.secret: The secret used to sign the JWT - should correlate to Mercure Hub configuration.

Note that the adminToken must be generated using the same alg and secret used in the Mercure Hub with the following payload.

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

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

eyJhbGciOiJIUzI1NiJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.mx2ROlYlE1rp7udoDy-WCdnpLdPuKWzDxoBJXGMK4OE

Usage

The Mercure Provider gives you access to two classes.

  • Update: This class is used to publish updates to the hub.
  • Token: This class is used to generate a token for authentication.

You can easily send an update to the hub using the Update.send method.

import { Update } from '@ioc:Setten/Mercure';

Update.send('/users/1', { ... });

The send method takes three arguments.

  • topic: The topic to publish the update to.
  • data: The data to publish.
  • isPrivate: Whether the update is private or not.

More information on the topic and data arguments can be found in the Mercure documentation.

Frontend

The frontend can listen to changes using the standard EventSource web interface.

const url = new URL(/*  Mercure Endpoint */)
url.searchParams.append('topic', '/users/1')

const eventSource = new EventSource(url)
eventSource.onmessage = (event) => {
  console.log(event.data)
}

More information on the topic can be found in the Mercure documentation.

Authentication

You may want to send private messages. To do so, you need to set the update as private using the third argument of the Update.send method, and authenticate the client using a JWT stored in a cookie.

You can generate the JWT using the Token class.

import { Token } from '@ioc:Setten/Mercure';

// Generating the token allowing the user to listen on private events
// send to `/users/1`.
const token = await Token.generate({
  subscribe: ['/users/1'],
})

// Adding the token in a cookie name `mercureAuthorization`.
response.append(
  'set-cookie',
  `mercureAuthorization=${token}; Domain=.setten.io; Path=/.well-known/mercure; HttpOnly`
)

The cookie must be named mercureAuthorization and must be not encoded by AdonisJS (you cannot use response.cookie() at the moment for that reason).

Note that the Mercure Hub must run on the same domain as the client since cookies cannot be shared cross-domain.

Once done, you have to change your client's connection to use cookies.

const eventSource = new EventSource(url, { withCredentials: true })

More information on the topic can be found in the Mercure documentation.