Skip to content

Commit

Permalink
Update README.md (#392)
Browse files Browse the repository at this point in the history
docs(readme): fix readme

Fix PubNub client configuration and listener documentation.
  • Loading branch information
techwritermat authored Jul 18, 2024
1 parent e8b2256 commit cac7bc7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 38 deletions.
6 changes: 3 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
changelog:
- date: 2024-07-04
- date: 2024-07-18
version: v8.2.5
changes:
- type: bug
text: "Fix issue because of which `signals` sent as `string` not handler properly."
- type: improvement
text: "Fix PubNub client configuration and listener documentation."
- date: 2024-06-17
version: v8.2.4
changes:
Expand Down
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## v8.2.5
July 04 2024
July 18 2024

#### Modified
- Fix PubNub client configuration and listener documentation.


#### Fixed
- Fix issue because of which `signals` sent as `string` not handler properly. Fixed the following issues reported by [@roman-rr](https://github.com/roman-rr): [#387](https://github.com/pubnub/javascript/issues/387).

## v8.2.4
June 17 2024
Expand Down
122 changes: 90 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,65 +35,123 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2

```javascript
pubnub = new PubNub({
publishKey : "myPublishKey",
subscribeKey : "mySubscribeKey",
uuid: "myUniqueUUID"
})
publishKey: 'myPublishKey',
subscribeKey: 'mySubscribeKey',
userId: 'myUniqueUserId',
});
```

## Add event listeners

```javascript
pubnub.addListener({
// create a subscription from a channel entity
const channel = pubnub.channel('my_channel');
const subscription = channel.subscription();
subscription.subscribe();
// Event-specific listeners
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
subscription.onSignal = (signalEvent) => { console.log("Signal event: ", signalEvent); };
subscription.onObjects = (objectsEvent) => { console.log("Objects event: ", objectsEvent); };
subscription.onMessageAction = (messageActionEvent) => { console.log("Message Action event: ", messageActionEvent); };
subscription.onFile = (fileEvent) => { console.log("File event: ", fileEvent); };
// Generic listeners
subscription.addListener({
// Messages
message: function (m) {
// handle messages
const channelName = m.channel; // Channel on which the message was published
const channelGroup = m.subscription; // Channel group or wildcard subscription match (if exists)
const pubTT = m.timetoken; // Publish timetoken
const msg = m.message; // Message payload
const publisher = m.publisher; // Message publisher
},
// Presence
// requires a subscription with presence
presence: function (p) {
// handle presence
const action = p.action; // Can be join, leave, state-change, or timeout
const channelName = p.channel; // Channel to which the message belongs
const occupancy = p.occupancy; // Number of users subscribed to the channel
const state = p.state; // User state
const channelGroup = p.subscription; // Channel group or wildcard subscription match, if any
const publishTime = p.timestamp; // Publish timetoken
const timetoken = p.timetoken; // Current timetoken
const uuid = p.uuid; // UUIDs of users who are subscribed to the channel
},
// Signals
signal: function (s) {
// handle signals
const channelName = s.channel; // Channel to which the signal belongs
const channelGroup = s.subscription; // Channel group or wildcard subscription match, if any
const pubTT = s.timetoken; // Publish timetoken
const msg = s.message; // Payload
const publisher = s.publisher; // Message publisher
},
// App Context
objects: (objectEvent) => {
// handle objects
const channel = objectEvent.channel; // Channel to which the event belongs
const channelGroup = objectEvent.subscription; // Channel group
const timetoken = objectEvent.timetoken; // Event timetoken
const publisher = objectEvent.publisher; // UUID that made the call
const event = objectEvent.event; // Name of the event that occurred
const type = objectEvent.type; // Type of the event that occurred
const data = objectEvent.data; // Data from the event that occurred
},
// Message Reactions
messageAction: function (ma) {
// handle message actions
const channelName = ma.channel; // Channel to which the message belongs
const publisher = ma.publisher; // Message publisher
const event = ma.event; // Message action added or removed
const type = ma.data.type; // Message action type
const value = ma.data.value; // Message action value
const messageTimetoken = ma.data.messageTimetoken; // Timetoken of the original message
const actionTimetoken = ma.data.actionTimetoken; // Timetoken of the message action
},
// File Sharing
file: function (event) {
// handle files
},
status: function (s) {
// handle status
},
const channelName = event.channel; // Channel to which the file belongs
const channelGroup = event.subscription; // Channel group or wildcard subscription match (if exists)
const publisher = event.publisher; // File publisher
const timetoken = event.timetoken; // Event timetoken
const message = event.message; // Optional message attached to the file
const fileId = event.file.id; // File unique id
const fileName = event.file.name;// File name
const fileUrl = event.file.url; // File direct URL
}
});
```

## Publish/subscribe

```javascript
var publishPayload = {
channel : "hello_world",
message: {
title: "greeting",
description: "This is my first message!"
}
const channel = pubnub.channel('my_channel');
const subscription = channel.subscription();
subscription.subscribe();
try {
const result = await pubnub.publish({
message: {
such: "object",
},
channel: "my_channel",
sendByPost: false, // true to send via post
storeInHistory: false, //override default Message Persistence options
meta: {
cool: "meta",
}, // publish extra meta with the request
});
} catch (status) {
console.log(status);
}
pubnub.publish(publishPayload, function(status, response) {
console.log(status, response);
})
pubnub.subscribe({
channels: ["hello_world"]
});
```

## Documentation

* [Build your first realtime JS app with PubNub](https://www.pubnub.com/docs/platform/quickstarts/javascript)
* [API reference for JavaScript (web)](https://www.pubnub.com/docs/web-javascript/pubnub-javascript-sdk)
* [API reference for JavaScript (Node.js)](https://www.pubnub.com/docs/nodejs-javascript/pubnub-javascript-sdk)
* [Build your first realtime JS app with PubNub](https://www.pubnub.com/tutorials/real-time-data-streaming/)
* [API reference for JavaScript](https://www.pubnub.com/docs/sdks/javascript/api-reference/publish-and-subscribe)

## Support

Expand Down

0 comments on commit cac7bc7

Please sign in to comment.