|
| 1 | +--- |
| 2 | +title: LiveObjects setup |
| 3 | +meta_description: "Install, authenticate and instantiate the LiveObjects on a channel." |
| 4 | +redirect_from: /docs/liveobjects/setup |
| 5 | +languages: |
| 6 | + - javascript |
| 7 | +--- |
| 8 | + |
| 9 | +blang[javascript]. |
| 10 | + Use these instructions to install the Ably SDK, authenticate, and configure the Objects plugin to start working with LiveObjects. |
| 11 | + |
| 12 | +h2(#authentication). Authentication |
| 13 | + |
| 14 | +An "API key":/docs/auth#api-keys is required to authenticate with Ably. API keys are used either to authenticate directly with Ably using "basic authentication":/docs/auth/basic, or to generate tokens for untrusted clients using "token authentication":/docs/auth/token. |
| 15 | + |
| 16 | +<aside data-type='important'> |
| 17 | +<p>The examples use "basic authentication":/docs/auth/basic to demonstrate features for convenience. In your own applications, basic authentication should never be used on the client-side as it exposes your Ably API key. Instead use "token authentication":/docs/auth/token. </p> |
| 18 | +</aside> |
| 19 | + |
| 20 | +"Sign up":https://ably.com/sign-up to Ably to create an API key in the "dashboard":https://ably.com/dashboard or use the "Control API":/docs/account/control-api to create an API key programmatically. |
| 21 | + |
| 22 | +API keys and tokens have a set of "capabilities":/docs/auth/capabilities assigned to them that specify which operations, such as @subscribe@ or @publish@ can be performed on which resources. To use LiveObjects, the API key requires the following capabilities: |
| 23 | + |
| 24 | +* Publish |
| 25 | +* Subscribe |
| 26 | + |
| 27 | +h2(#install). Install |
| 28 | + |
| 29 | +blang[javascript]. |
| 30 | + LiveObjects is available as part of the Ably Pub/Sub SDK via the dedicated Objects plugin. |
| 31 | + |
| 32 | +blang[javascript]. |
| 33 | + h3(#npm). NPM |
| 34 | + |
| 35 | + Install the Ably Pub/Sub SDK: |
| 36 | + |
| 37 | + ```[sh] |
| 38 | + npm install ably |
| 39 | + ``` |
| 40 | + |
| 41 | + Import the SDK and the Objects plugin into your project: |
| 42 | + |
| 43 | + ```[javascript] |
| 44 | + import * as Ably from 'ably'; |
| 45 | + import Objects from 'ably/objects'; |
| 46 | + ``` |
| 47 | + |
| 48 | +blang[javascript]. |
| 49 | + h3(#cdn). CDN |
| 50 | + |
| 51 | + Reference the Ably Pub/Sub SDK and the Objects plugin within your HTML file: |
| 52 | + |
| 53 | + ```[html] |
| 54 | + <script src="https://cdn.ably.com/lib/ably.min-2.js"></script> |
| 55 | + <script src="https://cdn.ably.com/lib/objects.umd.min-2.js"></script> |
| 56 | + <script> |
| 57 | + // Objects plugin is now available on the global object via the `AblyObjectsPlugin` property |
| 58 | + const Objects = window.AblyObjectsPlugin; |
| 59 | + </script> |
| 60 | + ``` |
| 61 | + |
| 62 | +h2(#instantiate). Instantiate a client |
| 63 | + |
| 64 | +blang[javascript]. |
| 65 | + Instantiate an Ably Realtime client from Pub/Sub SDK, providing the Objects plugin: |
| 66 | + |
| 67 | + ```[javascript] |
| 68 | + const realtimeClient = new Ably.Realtime({ key: '{{API_KEY}}', plugins: { Objects } }); |
| 69 | + ``` |
| 70 | + |
| 71 | +blang[javascript]. |
| 72 | + A "@ClientOptions@":/docs/api/realtime-sdk#client-options object may be passed to the Pub/Sub SDK instance to further customize the connection, however at a minimum you must set an API key and provide an Objects plugin so that the client can use LiveObjects functionality. |
| 73 | + |
| 74 | +h2(#create-channel). Create a channel |
| 75 | + |
| 76 | +LiveObjects are managed and persisted on a "realtime channels":/docs/channels, so you need to create a channel first. To use LiveObjects on a channel, you must set the correct "channel mode flags":/docs/channels/options#modes: |
| 77 | + |
| 78 | +* *OBJECT_SUBSCRIBE* - required to retrieve LiveObjects for a channel |
| 79 | +* *OBJECT_PUBLISH* - required to create new and modify existing LiveObjects on a channel |
| 80 | + |
| 81 | +blang[javascript]. |
| 82 | + |
| 83 | + ```[javascript] |
| 84 | + const channelOptions = { modes: ['OBJECT_SUBSCRIBE', 'OBJECT_PUBLISH'] }; |
| 85 | + const channel = realtimeClient.channels.get('my_objects_channel', channelOptions); |
| 86 | + ``` |
| 87 | + |
| 88 | +h2(#objects). Access Objects on a channel |
| 89 | + |
| 90 | +blang[javascript]. |
| 91 | + LiveObjects can be accessed on realtime channels via the "objects":/docs/api/realtime-sdk/channels#objects property. |
| 92 | + |
| 93 | +blang[javascript]. |
| 94 | + |
| 95 | + ```[javascript] |
| 96 | + const objects = channel.objects; |
| 97 | + ``` |
0 commit comments