|
| 1 | +# Migrating your existing bundle to nodecg-io |
| 2 | + |
| 3 | +This guide explains you how to migrate an existing nodecg bundle that already meets your requirements to use nodecg-io. |
| 4 | +nodecg-io manages service configuration and authentication so your bundle doesn't have to bother with it. |
| 5 | + |
| 6 | +If you are lucky and used the same library as we do for the service you want to migrate, nodecg-io is a drop-in replacement just doing authentication and configuration. |
| 7 | +If you have used another library you'll have to do a little more work to migrate to the library that our service uses. |
| 8 | +In that case make sure that the migration is worth the effort. |
| 9 | +You can use nodecg-io for one service and use your own library of choice for other services if you like to. |
| 10 | + |
| 11 | +## Install `nodecg-io-core` |
| 12 | + |
| 13 | +The first thing you need to do is to install `nodecg-io-core`. You can do this simply using this npm command: |
| 14 | + |
| 15 | +```shell |
| 16 | +npm install nodecg-io-core |
| 17 | +``` |
| 18 | + |
| 19 | +## Import `requireService` |
| 20 | + |
| 21 | +Now that you have `nodecg-io-core` installed in your bundle you can import the `requireService` function that it exposes: |
| 22 | + |
| 23 | +=== "TypeScript/ES Modules" |
| 24 | + |
| 25 | + ```typescript |
| 26 | + import { requireService } from "nodecg-io-core"; |
| 27 | + ``` |
| 28 | + |
| 29 | +=== "CommonJS" |
| 30 | + |
| 31 | + ```javascript |
| 32 | + const requireService = require("nodecg-io-core").requireService; |
| 33 | + ``` |
| 34 | + |
| 35 | +## Import Service type (TypeScript only) |
| 36 | + |
| 37 | +If you're using TypeScript you'll also need to install the package of the service that you want to use. |
| 38 | +You need it to be able to import its type for typesafety. |
| 39 | + |
| 40 | +This example uses `twitch-chat` as an example but you can just replace it with the name of the service you need. |
| 41 | + |
| 42 | +=== "TypeScript" |
| 43 | + |
| 44 | + ```shell |
| 45 | + npm install nodecg-io-twitch-chat |
| 46 | + ``` |
| 47 | + |
| 48 | + ```typescript |
| 49 | + import { TwitchChatServiceClient } from "nodecg-io-twitch-chat"; |
| 50 | + ``` |
| 51 | + |
| 52 | +Note: You can get the name of the type by using autocomplete in your editor or by looking it up in the service sample. |
| 53 | + |
| 54 | +## Require the service |
| 55 | + |
| 56 | +Now you can finally tell nodecg-io that you want to get access to a service using the `requireService` function. |
| 57 | +You'll have to pass your nodecg instance which is used to identify your bundle and the name of the service you want. |
| 58 | +In case of TypeScript you'll also need to provide the type of the service. |
| 59 | + |
| 60 | +=== "TypeScript" |
| 61 | + |
| 62 | + ```typescript |
| 63 | + const twitchChat = requireService<TwitchChatServiceClient>(nodecg, "twitch-chat"); |
| 64 | + ``` |
| 65 | + |
| 66 | +=== "JavaScript" |
| 67 | + |
| 68 | + ```javascript |
| 69 | + const twitchChat = requireService(nodecg, "twitch-chat"); |
| 70 | + ``` |
| 71 | + |
| 72 | +## Accessing the service client |
| 73 | + |
| 74 | +### Using `onAvailable` and `onUnavailable` handlers |
| 75 | + |
| 76 | +You can setup handlers that are executed when the user assigns a service instance to your bundle, removes the assignment or when the service client got updated in some way. |
| 77 | + |
| 78 | +Handlers added with `onAvailable` will get called if there was a change and you got a client: |
| 79 | + |
| 80 | +```typescript |
| 81 | +// This is the variable with the return value of requireService(). |
| 82 | +// You may want to change the variable name for the service you are using. |
| 83 | +twitchChat.onAvailable(client => { |
| 84 | + nodecg.log.info("Client was set"); |
| 85 | + // You can use the passed client in here to e.g. setup handlers on the client |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +`onAvailable` is especially useful to add event handlers to clients. |
| 90 | +E.g. if you want to react to donations or chat messages you can add event handlers for these here. |
| 91 | + |
| 92 | +Handlers added with `onUnavailable` will get called if your bundle was not assigned a service instance or if there was an error in service client creation: |
| 93 | + |
| 94 | +```typescript |
| 95 | +twitchChat.onUnavailable(() => { |
| 96 | + nodecg.log.info("Client was unset"); |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +### Using `getClient` |
| 101 | + |
| 102 | +Instead of callbacks you can also get access to the client directly: |
| 103 | + |
| 104 | +```typescript |
| 105 | +const twitchChatClient = twitchChat.getClient(); |
| 106 | +``` |
| 107 | + |
| 108 | +`getClient` will return the client if your bundle has an assigned service instance that has produced an service client without error |
| 109 | +and will return `undefined` otherwise. |
| 110 | +This is useful for when you want to use the client due to some external event or from `onAvailable` handlers of other services. |
0 commit comments