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

web-pubsub-graphql-subscribe

v1.0.1

Published

use Azure WebPub service in GraphQL subscriptions

Downloads

10

Readme

webpubsub-graphql-subscribe

Introduction

Microsoft Azure Web PubSub is a real-time messaging cloud service. In GraphQL, subscriptions are long-lasting GraphQL read operations that can update their result whenever a particular server-side event occurs. And it is usually implemented with WebSocket protocol.

Firstly, this package helps developers use Microsoft Azure WebPub service to avoid server-side maintenance of WebSocket connections between users clients and GraphQL server caused by subscriptions query from clients.

Secondly, this package provides a replacement for PubSub using Azure Web PubSub service. PubSub is an in-memory event-publishing system provided by Apollo server.

Prepare

  1. Create a Microsoft Azure Web PubSub resource instance. Details are Here.

  2. Use ngrok to expose our local endpoint to the public Internet

Notice: make sure the region of your Azure Web PubSub resource and the region of ngrok tunnel server are the same. For Example, if your Azure Web PubSub instance is located in Asia Pacific (ap) region , run your ngrok with parameter --region=ap as below. Ngrok documents shows more location settings.

ngrok http --region=ap 8888 

Then you'll get a forwarding endpoint http://{ngrok-id}.ngrok.io like http://1bff94a2f246.ap.ngrok.io

  1. Set Event Handler in Azure Web PubSub service. Go to Azure portal -> Find your Web PubSub resource -> Settings. Add two new hub settings as below. Replace the {ngrok-id} to yours.

| Hub Name: graphql_main | | | | ---------------------------------------------- | ------------------ | ------------------------------ | | URL Template | User Event Pattern | System Events | | http://{ngrok-id}.ngrok.io/wps-services/main | * | connect,connected,disconnected |

| Hub Name: graphql_pubsub | | | | ---------------------------------------------- | ------------------ | ------------------------------ | | URL Template | User Event Pattern | System Events | | http://{ngrok-id}.ngrok.io/wps-services/pubsub | * | No system Events is selected |

Deploy a demo

  1. Clone this repository and install required package
git clone https://github.com/xingsy97/webpubsub-graphql-subscribe
cd webpubsub-graphql-subscribe
npm install
  1. replace <web-pubsub-connection-string> in demos/demo.js Line 7 to your own Azure Web PubSub connection string

  2. Compile && Run the demo

npm run compile && npm run demo
  1. Open your web browser like Google Chrome, visit http://localhost:4000/graphql. Copy the following GraphQL query to the left panel.
subscription sampleSubscription {
  numberIncremented
}

Then click the play button and watch the right pannel.

Usage

src/demos/demo-without-webpubsub.ts is a sample show Subscriptions in Apollo Server without Azure WebPub service. Follow these instructions, we will apply Web Pubsub to this sample.

  1. define a variable to store Web PubSub connection string
const webpubsub_conn_string = "<webpubsub-connection-string>"
  1. replace original in-memory event system PubSub to our WpsPubSub based on Web PubSub.

Old:

const pubsub = new PubSub();

New:

const pubsub = new WpsPubSub(webpubsub_conn_string);
  1. use create_webpubsub_subscribe_server exported by our package to create a subscription server

Old:

SubscriptionServer.create(
  { schema, execute, subscribe },
  { server: httpServer, path: apolloServer.graphqlPath }
);

New:

await create_webpubsub_subscribe_server(apolloServer, schema, pubsub, webpubsub_conn_string);

Now your subscription server inside Apollo Server is based on Azure Web PubSub service. Refer to src/demos/demo.ts for complete source code.

Implementations

  • class WpsWebSocketServer

    • Original GraphQL subscriptions implementation starts up a WebSocket server which listens to clients and maintains WebSocket connections in server-side.
    • This class replaces original WebSocket.Server and communicate between the server and WebPub service using HTTP protocol.
    • And clients use WebSocket communicates with WebPub service rather than directly communicate with our server by WebSocket.
  • class WpsPubSub

    • It implements the PubSubEngine Interface from the graphql-subscriptions package using Azure Web PubSub service.
    • It replaces the original in-memory event system PubSub and allows you to connect your subscriptions manager to an Azure Web PubSub service to support multiple subscription manager instances.