Often you will need to get information from the GumbandSDK Gumband Cloud through the Gumband SDK, and in order to do this the SDK will first need to be ready for you to use its methods and propertiesauthenticate itself with the Gumband Cloud. Until authentication is complete, the SDK will be marked as “not ready”. This article will help you understand the two ways you can discern if the SDK is ready to interaction/drive your exhibit.
Using a Singleton
First, we are going to assume you have followed the “Best Practice: Wrapping Gumband SDK as A Singleton Class”. With this pattern you have ensured that no matter where or when you “new-up” your GumbandService class you will get the same object in-memory.
Once you have a reference to the singleton you can identify if an Exhibit is ready in 2 different ways.
get settings, dispatch analytic events, etc.
Using an Observer Pattern to listen for READY events
The SDK emits READY
from it’s its event emitter once all health checks have been performedit has successfully authenticated with the Gumband Cloud and a websocket connection has been established. You can listen for this event within your singleton in this manner:
Code Block | ||
---|---|---|
| ||
// FILE: your-gumband-project/gumband/gumband-service-wrapper.js const { Gumband, Sockets } = require("@deeplocal/gumband-node-sdk"); /** * A class that wraps the Gumband SDK and handles websocket messages * that come from the Gumband Cloud. */ class GumbandService { /** * A reference to the Gumband SDK instance. */ gumbandSDK; // Static Variable to hold the singleton instance static instance; constructor(window) { if(GumbandService.instance) { return GumbandService.instance; } this.gumbandSDK = new Gumband(...constructorArgs); this.addSDKListeners(); Gumband.instance = this. } /** * Add listeners to the Gumband SDK to handle messages from the Gumband Cloud. */ addSDKListeners() { this.gumbandSDK.on(Sockets.READY, async (manifest) => { // Call whatever you need to happen once the exhibit is ready. }) } } module.exports = { GumbandService }; |
...
Code Block |
---|
// FILE: your-gumband-project/app/main.js const { GumbandService } = require('../gumband/gumband-service-wrapper'); const gb = new GumbandService() // 👈 Get reference to the singleton; if (gb.gumbandSDK.isReady()) { console.log('Gumband SDK is ready for operation'); } else { console.log('Gumband SDK is not yet ready, check later, or listen for READY event'); } |
Additional Notes
Intermittent Connection
If the exhibit disconnects we emit a an EXHIBIT_OFFLINE event. Listening to this will let you know you lost connection. However after a short period of time the exhibit Gumband SDK will automatically begin trying to authenticate & reconnect at a set interval. If it fails it will emit ERROR and then queue another attempt, if authentication & reconnection succeeds then the SDK will emit "READY" again.