-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathclient.js
More file actions
75 lines (67 loc) · 2.88 KB
/
client.js
File metadata and controls
75 lines (67 loc) · 2.88 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
console.warn("\n1. Make sure server is running i.e. `npm run server`")
const Pusher = require('pusher-js');
const pusherRealtime = new Pusher('appid.keyid', { // replace with first part of API key before :
wsHost: 'realtime-pusher.ably.io',
wsPort: 443,
disableStats: false,
useTLS: true,
authEndpoint: 'http://localhost:5000/ably/auth', // deprecated
});
pusherRealtime.connection.bind('connected', () => {
console.log('connected');
});
pusherRealtime.connection.bind('disconnected', () => {
console.log('disconnected');
});
pusherRealtime.connection.bind("error", (error) => {
console.error(error);
});
pusherRealtime.bind_global((eventName, data)=> {
console.log("Global eventName-" + eventName + " data-" + JSON.stringify(data));
});
// Public channel -> Doesn't make any explicit request for authorization
const public_channel = pusherRealtime.subscribe('public-channel');
public_channel.bind("pusher:subscription_succeeded", () => {
console.log('subscribed to public-channel')
});
public_channel.bind("pusher:subscription_error", (err) => {
console.error('public channel subscription error', err)
});
public_channel.bind_global((eventName, data)=> {
console.log("public channel :: eventName-" + eventName + " data-" + JSON.stringify(data));
});
// Private channel -> requests { 'auth' : 'token'} from /ably/auth
const private_channel = pusherRealtime.subscribe('private-channel');
private_channel.bind("pusher:subscription_succeeded", () => {
console.log('subscribed to private-channel')
});
private_channel.bind("pusher:subscription_error", (err) => {
console.error('private channel subscription error', err)
});
private_channel.bind_global((eventName, data)=> {
console.log("private channel :: eventName-" + eventName + " data-" + JSON.stringify(data));
});
// Presence channel -> requests { 'auth' : 'token', 'channelData' : {'userId': '', userInfo: ''}} from /ably/auth
const presence_channel = pusherRealtime.subscribe('presence-channel');
presence_channel.bind("pusher:subscription_succeeded", (members) => {
console.log('\n###subscribed to presence-channel###')
console.log(`members count :: ${members.count}`)
members.each((member) => {
console.log(member.id, member.info)
});
console.log('#########\n')
});
presence_channel.bind("pusher:member_added", (member) => {
console.log('\nmember added', member);
console.log('updated members', presence_channel.members.count)
});
presence_channel.bind("pusher:member_removed", (member) => {
console.log('\nmember removed', member)
console.log('updated members', presence_channel.members.count)
});
presence_channel.bind("pusher:subscription_error", (err) => {
console.error('presence channel subscription error', err)
});
presence_channel.bind_global((eventName, data)=> {
console.log("presence channel :: eventName-" + eventName + " data-" + JSON.stringify(data));
});