Skip to content

Commit cac7bc7

Browse files
Update README.md (#392)
docs(readme): fix readme Fix PubNub client configuration and listener documentation.
1 parent e8b2256 commit cac7bc7

File tree

3 files changed

+98
-38
lines changed

3 files changed

+98
-38
lines changed

.pubnub.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
changelog:
3-
- date: 2024-07-04
3+
- date: 2024-07-18
44
version: v8.2.5
55
changes:
6-
- type: bug
7-
text: "Fix issue because of which `signals` sent as `string` not handler properly."
6+
- type: improvement
7+
text: "Fix PubNub client configuration and listener documentation."
88
- date: 2024-06-17
99
version: v8.2.4
1010
changes:

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
## v8.2.5
2-
July 04 2024
2+
July 18 2024
3+
4+
#### Modified
5+
- Fix PubNub client configuration and listener documentation.
6+
37

4-
#### Fixed
5-
- 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).
68

79
## v8.2.4
810
June 17 2024

README.md

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,65 +35,123 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
3535
3636
```javascript
3737
pubnub = new PubNub({
38-
publishKey : "myPublishKey",
39-
subscribeKey : "mySubscribeKey",
40-
uuid: "myUniqueUUID"
41-
})
38+
publishKey: 'myPublishKey',
39+
subscribeKey: 'mySubscribeKey',
40+
userId: 'myUniqueUserId',
41+
});
4242
```
4343

4444
## Add event listeners
4545

4646
```javascript
47-
pubnub.addListener({
47+
// create a subscription from a channel entity
48+
const channel = pubnub.channel('my_channel');
49+
const subscription = channel.subscription();
50+
subscription.subscribe();
51+
52+
// Event-specific listeners
53+
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
54+
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
55+
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
56+
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
57+
subscription.onSignal = (signalEvent) => { console.log("Signal event: ", signalEvent); };
58+
subscription.onObjects = (objectsEvent) => { console.log("Objects event: ", objectsEvent); };
59+
subscription.onMessageAction = (messageActionEvent) => { console.log("Message Action event: ", messageActionEvent); };
60+
subscription.onFile = (fileEvent) => { console.log("File event: ", fileEvent); };
61+
62+
// Generic listeners
63+
subscription.addListener({
64+
// Messages
4865
message: function (m) {
49-
// handle messages
66+
const channelName = m.channel; // Channel on which the message was published
67+
const channelGroup = m.subscription; // Channel group or wildcard subscription match (if exists)
68+
const pubTT = m.timetoken; // Publish timetoken
69+
const msg = m.message; // Message payload
70+
const publisher = m.publisher; // Message publisher
5071
},
72+
// Presence
73+
// requires a subscription with presence
5174
presence: function (p) {
52-
// handle presence
75+
const action = p.action; // Can be join, leave, state-change, or timeout
76+
const channelName = p.channel; // Channel to which the message belongs
77+
const occupancy = p.occupancy; // Number of users subscribed to the channel
78+
const state = p.state; // User state
79+
const channelGroup = p.subscription; // Channel group or wildcard subscription match, if any
80+
const publishTime = p.timestamp; // Publish timetoken
81+
const timetoken = p.timetoken; // Current timetoken
82+
const uuid = p.uuid; // UUIDs of users who are subscribed to the channel
5383
},
84+
// Signals
5485
signal: function (s) {
55-
// handle signals
86+
const channelName = s.channel; // Channel to which the signal belongs
87+
const channelGroup = s.subscription; // Channel group or wildcard subscription match, if any
88+
const pubTT = s.timetoken; // Publish timetoken
89+
const msg = s.message; // Payload
90+
const publisher = s.publisher; // Message publisher
5691
},
92+
// App Context
5793
objects: (objectEvent) => {
58-
// handle objects
94+
const channel = objectEvent.channel; // Channel to which the event belongs
95+
const channelGroup = objectEvent.subscription; // Channel group
96+
const timetoken = objectEvent.timetoken; // Event timetoken
97+
const publisher = objectEvent.publisher; // UUID that made the call
98+
const event = objectEvent.event; // Name of the event that occurred
99+
const type = objectEvent.type; // Type of the event that occurred
100+
const data = objectEvent.data; // Data from the event that occurred
59101
},
102+
// Message Reactions
60103
messageAction: function (ma) {
61-
// handle message actions
104+
const channelName = ma.channel; // Channel to which the message belongs
105+
const publisher = ma.publisher; // Message publisher
106+
const event = ma.event; // Message action added or removed
107+
const type = ma.data.type; // Message action type
108+
const value = ma.data.value; // Message action value
109+
const messageTimetoken = ma.data.messageTimetoken; // Timetoken of the original message
110+
const actionTimetoken = ma.data.actionTimetoken; // Timetoken of the message action
62111
},
112+
// File Sharing
63113
file: function (event) {
64-
// handle files
65-
},
66-
status: function (s) {
67-
// handle status
68-
},
114+
const channelName = event.channel; // Channel to which the file belongs
115+
const channelGroup = event.subscription; // Channel group or wildcard subscription match (if exists)
116+
const publisher = event.publisher; // File publisher
117+
const timetoken = event.timetoken; // Event timetoken
118+
119+
const message = event.message; // Optional message attached to the file
120+
const fileId = event.file.id; // File unique id
121+
const fileName = event.file.name;// File name
122+
const fileUrl = event.file.url; // File direct URL
123+
}
69124
});
70125
```
71126

72127
## Publish/subscribe
73128

74129
```javascript
75-
var publishPayload = {
76-
channel : "hello_world",
77-
message: {
78-
title: "greeting",
79-
description: "This is my first message!"
80-
}
130+
const channel = pubnub.channel('my_channel');
131+
const subscription = channel.subscription();
132+
subscription.subscribe();
133+
134+
try {
135+
const result = await pubnub.publish({
136+
message: {
137+
such: "object",
138+
},
139+
channel: "my_channel",
140+
sendByPost: false, // true to send via post
141+
storeInHistory: false, //override default Message Persistence options
142+
meta: {
143+
cool: "meta",
144+
}, // publish extra meta with the request
145+
});
146+
} catch (status) {
147+
console.log(status);
81148
}
82-
83-
pubnub.publish(publishPayload, function(status, response) {
84-
console.log(status, response);
85-
})
86-
87-
pubnub.subscribe({
88-
channels: ["hello_world"]
89-
});
90149
```
91150

92151
## Documentation
93152

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

98156
## Support
99157

0 commit comments

Comments
 (0)