https://github.com/stephenlb/pubnub-sse
PubNub SSE SDK
https://github.com/stephenlb/pubnub-sse
Last synced: 4 months ago
JSON representation
PubNub SSE SDK
- Host: GitHub
- URL: https://github.com/stephenlb/pubnub-sse
- Owner: stephenlb
- Created: 2024-09-25T21:36:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-08-19T18:37:47.000Z (10 months ago)
- Last Synced: 2025-09-27T00:41:02.652Z (9 months ago)
- Language: Go
- Size: 680 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PubNub SSE
Easy to use PubNub SDK with SSE enabled by default.
### NPM Install
```shell
npm install pubnub-sse
```
### Run a Quick Demo
```shell
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.

Setup the SDK as follows in the example.
### Subscription Async Iterator
```javascript
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
```javascript
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.
```javascript
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
```html
// 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);
```