Live Streaming / Stream Setup

Overview

This page will instruct you how to set up the streaming connection with the Ably SDK.

We will provide a general overview about installing the Ably SDK, setting up a connection to Ably, subscribing to a channel and requesting messages to be sent to that channel.

Download the SDK

Please download the Ably SDK of your choice to connect with our streaming partner's service. Please refer to the Ably Documentation for details about your Ably SDK.

Connect to Ably

Using the information you received from /auth, you can initialize the Ably SDK's realtime interface. This is how we do it using JavaScript:

1const { Realtime } = require('ably')
2
3// TODO: Perform call to /auth of the live streaming API, store token and
4// user_id in local variables.
5const connection = new Realtime({
6    token: token,
7    transportParams: { remainPresentFor: 1000 }
8})

Using the variable connection you can now move on to set up the receiving end of the streaming connection.

Setup Messaging Channel

Live messages will be broadcast to a user-specific channel within Ably. So, let's set up that channel and subscribe to all the messages that will be sent to us. Using the user_id from /auth, we can create the channel and then subscribe to be notified about incoming messages.

Using JavaScript, this can look like this:

1// HINT: `user_id` will be initialized using a call to `/auth`.
2const channelMessages = connection.channels.get(user_id)
3channelMessages.subscribe((message) => {
4  const { name, data } = message
5  console.log("message:", name, data)
6})
7console.log("awaiting messages…")

Now all that's left is telling the streaming interface which kinds of messages we like to receive.

Streaming Limited to 4 Channels

To provide a great experience to all our customers, we restrict each user to a maximum of four channels. This provides you with one live connection and in case this connection receives a timeout, you can immediately create a new connection. This is useful e.g. when a mobile phone drops a Wifi connection and switches to Cellular.

If more than four channels are connected, we reserve the right to terminate connections or prevent you from creating more connections.

Request Messages

Using a secondary channel, we can request messages to be delivered to our application.

First we set up the channel for requesting messages. Again, in JavaScript it looks like this:

1const subscriptions = connection.channels.get(`${user_id}.subscriptions`)

The next, and final, step will be to learn requesting live quotes for instruments ↗.