-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.js
39 lines (33 loc) · 1.09 KB
/
node.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
const Ably = require("ably");
// Create the client
const ably = new Ably.Realtime.Promise({
authUrl: "https://ably.com/ably-auth/token/docs",
});
// wrapper for async functions
const ablyRealtimePromiseExample = async () => {
// Connect to Ably
await ably.connection.once("connected");
console.log("Connected to Ably!");
// get the channel to subscribe to
const channel = ably.channels.get("quickstart");
/*
Subscribe to a channel.
The promise resolves when the channel is attached
(and resolves synchronously if the channel is already attached).
*/
await channel.subscribe("greeting", (message) => {
console.log("Message is ==> " + message.data);
});
// Publish a message or two
await channel.publish("greeting", "hello!");
await channel.publish("greeting", "hello!");
await channel.publish("greeting", "hello!");
// wait 2s to receive all messages and then shut down
setTimeout(() => {
console.log("Closing connection...");
ably.close();
console.log("Closed the connection to Ably.");
}, 2000);
};
// call wrapper
ablyRealtimePromiseExample();