|
| 1 | +# Use service replicants |
| 2 | + |
| 3 | +Some services allow reading some data using [NodeCG Replicants](https://www.nodecg.dev/docs/classes/replicant). |
| 4 | + |
| 5 | +Replicants are especially useful for displaying simple information in graphics and dashboards. |
| 6 | +You can setup a replicant and directly use it for basic values inside your graphic/dashboard without the need for much extension code. |
| 7 | + |
| 8 | +TODO: explain for what replicants are useful |
| 9 | + |
| 10 | +Current list of services with replicant support: |
| 11 | + |
| 12 | +- StreamElements |
| 13 | + |
| 14 | +(more services with replicants will hopefully be added in the future) |
| 15 | + |
| 16 | +## Create and register replicants in your bundle extension |
| 17 | + |
| 18 | +To use a replicant of a service you must first depend on the service as usual. |
| 19 | +Please refer to [Your first bundle](./create_new_bundle.md) and [Migrating an existing bundle](./existing_bundle.md) for a guide on how to depend on nodecg-io services inside your NodeCG extension. |
| 20 | + |
| 21 | +With access to a service client you can create a replicant inside your bundle and pass it to the `setupReplicant` method of the service client to let it be filled with data by the service. |
| 22 | +Here's an example for the StreamElements service (replace the name with the service of the above list that you want to use): |
| 23 | + |
| 24 | +=== "TypeScript" |
| 25 | + |
| 26 | + ```typescript |
| 27 | + // Update your imports to include the type of the replicant data: |
| 28 | + import { StreamElementsReplicant, StreamElementsServiceClient } from "nodecg-io-streamelements"; |
| 29 | + |
| 30 | + module.exports = function(nodecg: NodeCG) { |
| 31 | + // Require service (as usual) |
| 32 | + const streamElements = requireService<StreamElementsServiceClient>(nodecg, "streamelements"); |
| 33 | + |
| 34 | + // Define replicant |
| 35 | + const streamElementsReplicant = nodecg.Replicant<StreamElementsReplicant>("myStreamElementsReplicant"); |
| 36 | + |
| 37 | + ... |
| 38 | + |
| 39 | + streamElements?.onAvailable((client) => { |
| 40 | + ... |
| 41 | + // Connect your replicant to this nodecg-io service instance. |
| 42 | + client.setupReplicant(streamElementsReplicant); |
| 43 | + }); |
| 44 | + } |
| 45 | + ``` |
| 46 | + |
| 47 | +=== "JavaScript" |
| 48 | + |
| 49 | + ```javascript |
| 50 | + module.exports = function(nodecg: NodeCG) { |
| 51 | + // Require service (as usual) |
| 52 | + const streamElements = requireService(nodecg, "streamelements"); |
| 53 | + |
| 54 | + // Define replicant |
| 55 | + const streamElementsReplicant = nodecg.Replicant("myStreamElementsReplicant"); |
| 56 | + |
| 57 | + ... |
| 58 | + |
| 59 | + streamElements?.onAvailable((client) => { |
| 60 | + ... |
| 61 | + // Connect your replicant to this nodecg-io service instance. |
| 62 | + client.setupReplicant(streamElementsReplicant); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + ``` |
| 67 | + |
| 68 | +## Use the service replicant |
| 69 | + |
| 70 | +In case you want to use the created replicant in your bundles extension you already have it declared and a reference to it in a variable. If you want to use it in a graphic or dashboard you'll need to declare the replicant with the same name there too. |
| 71 | + |
| 72 | +You can access the NodeCG replicant as usual. Use `replicant.value` to get the current state and `replicant.on("change", (newValue, oldValue) => { /* .... */ })` to be informed when the value of the replicant changes. |
| 73 | + |
| 74 | +To figure out what properties are available on the object value you can either look at the corresponding sample bundle or use autocomplete in your editor if you're using TypeScript. |
| 75 | + |
| 76 | + |
0 commit comments