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

pubnub-sse

v1.0.7

Published

PubNub SSE Protocol with some additions. SSE is basically utilizing the Transfer-Encoding: chunked HTTP header. The main difference is that we intentionally exclude `event: ` and `data: ` headers that are included in each message. We remove these headers

Downloads

404

Readme

PubNub SSE

Easy to use PubNub SDK with SSE enabled by default.

NPM Install

npm install pubnub-sse

Run a Quick Demo

git clone https://github.com/stephenlb/pubnub-sse.git
cd pubnub-sse
open index.html

Important Files:

  • pubnub.js PubNub SSE Streaming SDK
  • index.html Example app open to see a demo using streaming data.

PubNub SSE Screenshot

Setup the SDK as follows in the example.

Subscription Async Iterator

const pubnub = PubNub({ subscribeKey: 'demo', publishKey: 'demo'});
const subscription = pubnub.subscribe({channel: 'test'});

let count = 0;
for await (const msg of subscription) {
    console.log(msg);
    if (count++ >= 2) break;
}

Subscription Callback

const pubnub = PubNub({ subscribeKey: 'demo', publishKey: 'demo'});
const subscription = pubnub.subscribe({channel: 'test', messages: reciever});

function reciever(msg) {
    console.log(msg);
}

Encryption Example

Using the crypto-js common crypto lib for encryption/decryption. With added support for Cross-Platform messaging.

const PubNub = require('pubnub-sse');    // npm install pubnub-sse
const PubNubCryptor = require('pubnub'); // npm install pubnub

const pubkey = 'demo';
const subkey = 'demo';
const authKey = 'demo-auth-key';
const userId = 'test-user-id';
const cipherKey = 'pubnubenigma';

const pubnubInstance = PubNub({
    publishKey: pubkey,
    subscribeKey: subkey,
    authKey: authKey,
    userId: userId,
});

const pubnubCryptor = new PubNubCryptor({
    subscribeKey: subkey,
    publishKey: pubkey,
    uuid: userId,
    authKey: authKey,
    cipherKey: cipherKey,
});

const message = { text: "Hello World" };
const stringData = JSON.stringify(message);
const encrypted = pubnubCryptor.encrypt(stringData);
const channel = `test-channel-${Math.random()}`;
const subscription = pubnubInstance.subscribe({channel: channel});

// Publish
setTimeout(async () => {
    await pubnubInstance.publish({ channel: channel, message: encrypted});
}, 1000);

// Subscription Stream
for await (const encryptedMessage of subscription) {
    const decrypted = pubnubCryptor.decrypt(encryptedMessage);
    expect(encryptedMessage).to.equal(encrypted);
    expect(encryptedMessage).to.be.a('string');
    expect(decrypted).to.be.an('object');
    expect(message).to.deep.equal(decrypted);
    break;
}

subscription.unsubscribe();

Example Code

<script src="pubnub.js"></script>
<script>

// PubNub Setup
const userId = 'user-id';
const authKey = 'auth-key';
const channel = 'Commands';
const pubkey = 'pub-c-a88f5e0f-af28-4847-ad52-30495d0cbcb8';
const subkey = 'sub-c-a8cbfccb-676b-4034-9681-dfed95af8d7e';
const pubnub = PubNub({
    subscribeKey: subkey,
    publishKey: pubkey,
    authKey: authKey,
    userId: userId,
});

// Subscribe to Events "Starts the Stream"
const subscription = pubnub.subscribe({
    channel: channel,
    messages: receiveEvents,
});

// End subscription
// subscription.unsubscribe();

// Event Processing
function receiveEvents(event) {
    console.log(event);
}

// Publish Events Example
let eventId = 0;
setInterval(() => {
    pubnub.publish({
        channel: channel,
        message: {
            eventId: ++eventId, 
            userId: userId,
            data: `Event ${eventId} from ${userId} at ${new Date().toISOString()}`,
        },
    });
}, 1000);

</script>