Skip to content

Commit 8bf31a4

Browse files
mohitpubnubMohit Tejanipubnub-release-bot
authored
docs: snippet for pubnub apis. (#458)
* docs snippets for publish-subscribe, presence, message-persistence, app-context, mobile-push, access-manager, channel-groups. * fix: lint issue. * removed deprecation from deleteMessages() * code snippets for documentation. A separate file for impot and initialisation. * configuration code snippets * script to compile check the code snippets * fix: snippet compilation for lowest node version and build generated before snippet compilation happens * added missing iterativelyUpdateExistingMetadata * example with promises syntax demo * missing SubscriptionSet subscribe() basic usage example * added missing subscriptionSet example code snippets * added getFilterExpresssionBasicusage * revert changes of providing return type for addSubscriptionSet() method on subscriptionSet entity * added getting started code snippets * added missing generic listener syntax example * fix snippets indentation, GrantToken type fix by removing objects syntax support, Error handling refactor in code snippets * additional information about send file code snippet in node.js * * address Codacy quality checks suggestions. * Indentation as per other code base * Codacy suggestions * lint: codacy suggestion * removed deprected warning from deleteMessages API * added snippet for connect, disconnect and delete proxy * code snippets: formating setToken strings * fix dummy token length related format issue. * code snippet: error handling, additional comments * details about exception in getting started example. * PubNub SDK v9.6.2 release. --------- Co-authored-by: Mohit Tejani <[email protected]> Co-authored-by: PubNub Release Bot <[email protected]>
1 parent 913ebaf commit 8bf31a4

38 files changed

+2416
-17
lines changed

.github/workflows/run-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ jobs:
5050
run: |
5151
npm install
5252
npm run ${{ matrix.env }}
53+
- name: Test docs snippets syntax
54+
if: matrix.node == '18.18.0' && matrix.env == 'ci:node'
55+
run: npm run test:snippets
5356
- name: Cancel workflow runs for commit on error
5457
if: failure()
5558
uses: ./.github/.release/actions/actions/utils/fast-jobs-failure

.pubnub.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
---
22
changelog:
3+
- date: 2025-06-30
4+
version: v9.6.2
5+
changes:
6+
- type: improvement
7+
text: "Removed deprecation warning from deleteMessages method."
8+
- type: improvement
9+
text: "Added code snippets for docs."
310
- date: 2025-06-18
411
version: v9.6.1
512
changes:
@@ -1256,7 +1263,7 @@ supported-platforms:
12561263
- 'Ubuntu 14.04 and up'
12571264
- 'Windows 7 and up'
12581265
version: 'Pubnub Javascript for Node'
1259-
version: '9.6.1'
1266+
version: '9.6.2'
12601267
sdks:
12611268
- full-name: PubNub Javascript SDK
12621269
short-name: Javascript
@@ -1272,7 +1279,7 @@ sdks:
12721279
- distribution-type: source
12731280
distribution-repository: GitHub release
12741281
package-name: pubnub.js
1275-
location: https://github.com/pubnub/javascript/archive/refs/tags/v9.6.1.zip
1282+
location: https://github.com/pubnub/javascript/archive/refs/tags/v9.6.2.zip
12761283
requires:
12771284
- name: 'agentkeepalive'
12781285
min-version: '3.5.2'
@@ -1943,7 +1950,7 @@ sdks:
19431950
- distribution-type: library
19441951
distribution-repository: GitHub release
19451952
package-name: pubnub.js
1946-
location: https://github.com/pubnub/javascript/releases/download/v9.6.1/pubnub.9.6.1.js
1953+
location: https://github.com/pubnub/javascript/releases/download/v9.6.2/pubnub.9.6.2.js
19471954
requires:
19481955
- name: 'agentkeepalive'
19491956
min-version: '3.5.2'

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v9.6.2
2+
June 30 2025
3+
4+
#### Modified
5+
- Removed deprecation warning from deleteMessages method.
6+
- Added code snippets for docs.
7+
18
## v9.6.1
29
June 18 2025
310

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
2727
npm install pubnub
2828
```
2929
* or download one of our builds from our CDN:
30-
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.6.1.js
31-
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.6.1.min.js
30+
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.6.2.js
31+
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.6.2.min.js
3232
3333
2. Configure your keys:
3434

dist/web/pubnub.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4957,7 +4957,7 @@
49574957
return base.PubNubFile;
49584958
},
49594959
get version() {
4960-
return '9.6.1';
4960+
return '9.6.2';
49614961
},
49624962
getVersion() {
49634963
return this.version;
@@ -16170,7 +16170,6 @@
1617016170
*
1617116171
* @returns Asynchronous delete messages response or `void` in case if `callback` provided.
1617216172
*
16173-
* @deprecated
1617416173
*/
1617516174
deleteMessages(parameters, callback) {
1617616175
return __awaiter(this, void 0, void 0, function* () {

dist/web/pubnub.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-snippets/access-manager.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import PubNub, { PubNubError } from '../lib/types';
2+
3+
const pubnub = new PubNub({
4+
publishKey: 'demo',
5+
subscribeKey: 'demo',
6+
userId: 'myUniqueUserId',
7+
});
8+
9+
// snippet.grantTokenVariousResources
10+
try {
11+
const token = await pubnub.grantToken({
12+
ttl: 15,
13+
authorized_uuid: 'my-authorized-uuid',
14+
resources: {
15+
channels: {
16+
'channel-a': {
17+
read: true,
18+
},
19+
'channel-b': {
20+
read: true,
21+
write: true,
22+
},
23+
'channel-c': {
24+
read: true,
25+
write: true,
26+
},
27+
'channel-d': {
28+
read: true,
29+
write: true,
30+
},
31+
},
32+
groups: {
33+
'channel-group-b': {
34+
read: true,
35+
},
36+
},
37+
uuids: {
38+
'uuid-c': {
39+
get: true,
40+
},
41+
'uuid-d': {
42+
get: true,
43+
update: true,
44+
},
45+
},
46+
},
47+
});
48+
} catch (error) {
49+
console.error(
50+
`Grant token error: ${error}.${
51+
(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
52+
}`,
53+
);
54+
}
55+
// snippet.end
56+
57+
// snippet.grantTokenUsingRegEx
58+
try {
59+
const token = await pubnub.grantToken({
60+
ttl: 15,
61+
authorized_uuid: 'my-authorized-uuid',
62+
patterns: {
63+
channels: {
64+
'^channel-[A-Za-z0-9]$': {
65+
read: true,
66+
},
67+
},
68+
},
69+
});
70+
} catch (error) {
71+
console.error(
72+
`Grant token error: ${error}.${
73+
(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
74+
}`,
75+
);
76+
}
77+
// snippet.end
78+
79+
// snippet.grantTokenRegExAndResources
80+
try {
81+
const token = await pubnub.grantToken({
82+
ttl: 15,
83+
authorized_uuid: 'my-authorized-uuid',
84+
resources: {
85+
channels: {
86+
'channel-a': {
87+
read: true,
88+
},
89+
'channel-b': {
90+
read: true,
91+
write: true,
92+
},
93+
'channel-c': {
94+
read: true,
95+
write: true,
96+
},
97+
'channel-d': {
98+
read: true,
99+
write: true,
100+
},
101+
},
102+
groups: {
103+
'channel-group-b': {
104+
read: true,
105+
},
106+
},
107+
uuids: {
108+
'uuid-c': {
109+
get: true,
110+
},
111+
'uuid-d': {
112+
get: true,
113+
update: true,
114+
},
115+
},
116+
},
117+
patterns: {
118+
channels: {
119+
'^channel-[A-Za-z0-9]$': {
120+
read: true,
121+
},
122+
},
123+
},
124+
});
125+
} catch (error) {
126+
console.error(
127+
`Grant token error: ${error}.${
128+
(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
129+
}`,
130+
);
131+
}
132+
// snippet.end

docs-snippets/app-context.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import PubNub, { PubNubError } from '../lib/types';
2+
3+
const pubnub = new PubNub({
4+
publishKey: 'demo',
5+
subscribeKey: 'demo',
6+
userId: 'myUniqueUserId',
7+
});
8+
9+
// snippet.iterativelyUpdateExistingMetadata
10+
const channel = 'team.red';
11+
const name = 'Red Team';
12+
const description = 'The channel for Red team.';
13+
const customField = { visible: 'team' };
14+
15+
// Function to set and then update channel metadata
16+
try {
17+
const response = await pubnub.objects.setChannelMetadata({
18+
channel: channel,
19+
data: {
20+
name: name,
21+
description: description,
22+
custom: customField,
23+
},
24+
});
25+
console.log('The channel has been created with name and description.\n');
26+
27+
// Fetch current object with custom fields
28+
const currentObjectResponse = await pubnub.objects.getChannelMetadata({
29+
channel: channel,
30+
include: {
31+
customFields: true,
32+
},
33+
});
34+
const currentObject = currentObjectResponse.data;
35+
36+
// Initialize the custom field object
37+
const custom = currentObject.custom || {};
38+
39+
// Add or update the field
40+
custom['edit'] = 'admin';
41+
42+
// Writing the updated object back to the server
43+
const setResponse = await pubnub.objects.setChannelMetadata({
44+
channel: channel,
45+
data: {
46+
name: currentObject.name || '',
47+
description: currentObject.description || '',
48+
custom: custom,
49+
},
50+
});
51+
console.log('Object has been updated', setResponse);
52+
} catch (error) {
53+
console.error(
54+
`Set channel metadata error: ${error}.${
55+
(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
56+
}`,
57+
);
58+
}
59+
// snippet.end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import PubNub, { PubNubError } from '../../lib/types';
2+
3+
const pubnub = new PubNub({
4+
publishKey: 'demo',
5+
subscribeKey: 'demo',
6+
userId: 'myUniqueUserId',
7+
});
8+
9+
// snippet.accessManagerBasicUsage
10+
// Function to use grantToken method
11+
try {
12+
const token = await pubnub.grantToken({
13+
ttl: 15,
14+
authorized_uuid: 'my-authorized-uuid',
15+
resources: {
16+
channels: {
17+
'my-channel': {
18+
read: true,
19+
write: true,
20+
},
21+
},
22+
},
23+
});
24+
console.log('Granted Token:', token);
25+
} catch (error) {
26+
console.error(
27+
`Grant token error: ${error}.${
28+
(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
29+
}`,
30+
);
31+
}
32+
// snippet.end
33+
34+
// snippet.revokeTokenBasicUsage
35+
try {
36+
const response = await pubnub.revokeToken('p0AkFl043rhDdHRsple3KgQ3NwY6BDcENnctokenVzcqBDczaWdYIGOAeTyWGJI');
37+
} catch (error) {
38+
console.error(
39+
`Revoke token error: ${error}.${
40+
(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
41+
}`,
42+
);
43+
}
44+
// snippet.end
45+
46+
// snippet.parseTokenBasicUsage
47+
pubnub.parseToken('use-token-string-generated-by-grantToken()');
48+
// snippet.end
49+
50+
// snippet.setTokenBasicUsage
51+
pubnub.setToken('use-token-string-generated-by-grantToken()');
52+
// snippet.end

0 commit comments

Comments
 (0)