Installation
The Loby JavaScript SDK is loaded via a script tag from our CDN. No npm package or build step is required.
Adding the SDK
Add the following code to your HTML, typically before the closing </body> tag:
<script> window.lobyAsyncInit = async function() { await Loby.init({ clientId: 1234, version: 'v1' }) }; </script> <script async defer crossorigin="anonymous" src="https://js.loby.io/sdk.js"></script>
Understanding the Loading Pattern
The SDK uses an async initialization pattern:
- Define
lobyAsyncInit- This function is called automatically when the SDK has loaded - Load the script - The script is loaded asynchronously to avoid blocking page rendering
- SDK calls
lobyAsyncInit- Once loaded, the SDK invokes your initialization function
This pattern ensures the SDK is fully loaded before you attempt to use it.
Script Attributes
| Attribute | Purpose |
|---|---|
async | Loads the script asynchronously without blocking HTML parsing |
defer | Executes the script after the document has been parsed |
crossorigin="anonymous" | Enables CORS for the script request |
Waiting for SDK Ready
If you need to ensure the SDK is loaded before executing code elsewhere on your page, you can set a flag:
<script> window.lobyAsyncInit = async function() { await Loby.init({ clientId: 1234, version: 'v1' }) window.lobySDKLoaded = true }; </script> <script async defer crossorigin="anonymous" src="https://js.loby.io/sdk.js"></script>
Then in your application code:
async function waitForLoby() { while (!window.lobySDKLoaded) { await new Promise(resolve => setTimeout(resolve, 50)) } // SDK is ready }