@@ -35,65 +35,123 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
35
35
36
36
```javascript
37
37
pubnub = new PubNub({
38
- publishKey : " myPublishKey" ,
39
- subscribeKey : " mySubscribeKey" ,
40
- uuid: "myUniqueUUID"
41
- })
38
+ publishKey: ' myPublishKey' ,
39
+ subscribeKey: ' mySubscribeKey' ,
40
+ userId: 'myUniqueUserId',
41
+ });
42
42
```
43
43
44
44
## Add event listeners
45
45
46
46
``` 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
48
65
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
50
71
},
72
+ // Presence
73
+ // requires a subscription with presence
51
74
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
53
83
},
84
+ // Signals
54
85
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
56
91
},
92
+ // App Context
57
93
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
59
101
},
102
+ // Message Reactions
60
103
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
62
111
},
112
+ // File Sharing
63
113
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
+ }
69
124
});
70
125
```
71
126
72
127
## Publish/subscribe
73
128
74
129
``` 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);
81
148
}
82
-
83
- pubnub .publish (publishPayload, function (status , response ) {
84
- console .log (status, response);
85
- })
86
-
87
- pubnub .subscribe ({
88
- channels: [" hello_world" ]
89
- });
90
149
```
91
150
92
151
## Documentation
93
152
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 )
97
155
98
156
## Support
99
157
0 commit comments