forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-example.js
More file actions
85 lines (74 loc) · 3.02 KB
/
event-example.js
File metadata and controls
85 lines (74 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(function(){
// -----------------------------------------------------------------------
// PubNub Settings
// -----------------------------------------------------------------------
var channel = 'my-channel-name-here'
, pubnub = PUBNUB.init({
publish_key : 'demo',
subscribe_key : 'demo'
});
// -----------------------------------------------------------------------
// My-Example-Event Event
// -----------------------------------------------------------------------
pubnub.events.bind( 'My-Example-Event', function(message) {
message
} )
// -----------------------------------------------------------------------
// Presence Data Event
// -----------------------------------------------------------------------
pubnub.events.bind( 'presence', function(data) {
// Show User Count, etc.
data.occupancy
} );
// -----------------------------------------------------------------------
// Open Receiving Socket Connection
// -----------------------------------------------------------------------
pubnub.subscribe({
channel : channel,
presence : presence,
callback : receive
});
// -----------------------------------------------------------------------
// Presence Event
// -----------------------------------------------------------------------
function presence( data, envelope, source_channel ) {
pubnub.events.fire( 'presence', {
occupancy : data.occupancy,
user_ids : data.uuids,
channel : source_channel
});
}
// -----------------------------------------------------------------------
// Receive Data
// -----------------------------------------------------------------------
function receive( message, envelope, source_channel ) {
pubnub.events.fire( message.type, {
message : message,
channel : source_channel
});
}
// -----------------------------------------------------------------------
// New channels -AUTO MULTIPLEXES FOR YOU. v3.4 only.
// -----------------------------------------------------------------------
pubnub.subscribe({ channel : 'friend_ID1', callback : receive });
pubnub.subscribe({ channel : 'friend_ID2', callback : receive });
// Adding More Channels Using Array
pubnub.subscribe({
channel : ['friend_ID3','friend_ID4','friend_ID5','friend_ID6'],
callback : receive
});
// Adding More Channels Using Comma List
pubnub.subscribe({
channel : 'friend_ID7,friend_ID8,friend_ID9',
callback : receive
});
// -----------------------------------------------------------------------
// Send Data From Browser (only if publish_key key supplied)
// -----------------------------------------------------------------------
function send( type, data ) {
pubnub.publish({
channel : channel,
message : { type : type, data : data }
});
}
})();