-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-ably.js
56 lines (48 loc) · 1.58 KB
/
test-ably.js
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
"use strict";
const ApiKey = '[INSERT-API-KEY-HERE]'
const Async = require('async')
const Ably = require('ably')
const Prompt = require('prompt')
var startTime = new Date().getTime()
function getChannel() {
const client = new Ably.Realtime({ key: ApiKey, transports: ['web_socket'], log: { level: 2 } })
return client.channels.get('events')
}
const publishChannel = getChannel()
Prompt.start()
function promptForPublish() {
Prompt.get({ description: "Publish an event to all subscribers? [yes]" }, function(err, result) {
if (err) {
console.error("Error", err)
process.exit();
} else {
if (result.question.toLowerCase() === 'yes') {
console.log('Publishing count', clients.length)
publishChannel.publish('count', { count: clients.length, broadcastAt: new Date().getTime() }, function() {
setTimeout(promptForPublish, 1000);
})
} else {
process.exit();
}
}
});
}
const clients = []
Async.timesLimit(2500, 50, function(n, next) {
const channel = getChannel();
channel.once('attached', () => {
clients.push(n);
console.log('Client', n, 'attached')
channel.on('detached', () => {
clients.splice(clients.indexOf(n), 1)
console.log('Client', n, 'detached')
});
next()
});
channel.subscribe('count', (message) => {
console.log(message.data.count, 'clients connected. Elapsed time', Math.round(new Date().getTime() - message.data.broadcastAt), 'ms')
})
}, function() {
console.log('All clients connected in ', new Date().getTime() - startTime, 'ms')
promptForPublish();
})