Skip to content

Latest commit

 

History

History
77 lines (57 loc) · 1.43 KB

usage.md

File metadata and controls

77 lines (57 loc) · 1.43 KB

Ably Quickstart - Promises interface

This folder contains JavaScript source code for the Quickstart guide, using the promises interface.

Installation

For Node.js:

npm install ably

For JavaScript:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="//cdn.ably.com/lib/ably.min-1.js"></script>
  </head>
  <body>
  ...

Create the client

For Node.js client:

const Ably = require('ably/promises');
const ably = new Ably.Realtime.Promise({ authUrl: 'https://ably.com/ably-auth/token/docs' });

For JavaScript (browser client):

  <body>
  ...
  <script>
      const ably = new Ably.Realtime.Promise({
        authUrl: "/auth",
      });

Connect to Ably

await ably.connection.once('connected');
console.log('connected');

Subscribe to a Channel

// get the channel to subscribe to
const channel = ably.channels.get('quickstart');

// 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

await channel.publish('greeting', 'hello');

Close the connection

console.log("Closing connection...");
ably.close();
console.log('Closed the connection to Ably.');