Nats.io/nats-core UMD url to load into browser #758
Replies: 1 comment
-
The deno library is a TCP socket - if you want to run on the brower, you need nats.ws, which has been merged into nats.js - see https://github.com/nats-io/nats.js/blob/main/runtimes.md#running-in-the-browser |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I am using one SaaS platform tulip to connect with nats server and read the data, tulip has a feature called custom widget where you can write javascript code and html and css to create the visual.
I am writing a javascript code to load the nats library into the widget which is running on browser using url - "https://cdn.jsdelivr.net/npm/@nats-io/[email protected]/lib/nats.min.js"
code snippet is as follow:
async function loadNatsLibrary() { try { // Fetch the Nats.js library from CDN const response = await fetch( // "https://cdn.jsdelivr.net/npm/[email protected]/lib/nats-base-client/nats.min.js" "https://cdn.jsdelivr.net/npm/@nats-io/[email protected]/lib/nats.min.js" ); if (!response.ok) { throw new Error( "Failed to load Nats.js: ${response.status} ${response.statusText}" ); } const libraryCode = await response.text(); // Create a script element to load the library const scriptElement = document.createElement("script"); scriptElement.textContent = libraryCode; document.head.appendChild(scriptElement); console.log("hello"); // Wait a moment for the script to initialize await new Promise((resolve) => setTimeout(resolve, 100)); // Check if nats is available in the global scope if (typeof window.Nats === "undefined") { // Ifvisu a module // Some libraries expose themselves as a global variable with a different name // or through a global function. For Nats.js, it might be exposed as 'nats' if (typeof Nats !== "undefined") { window.Nats = Nats; } else { throw new Error( "nats.js library loaded but nats object not found in global scope" ); } } if (!window.Nats) throw new Error("Nats library not available after load"); console.log("NATS.js library loaded"); return true; } catch (error) { console.error("Failed to load NATS.js:", error); fireEvent("onError", "Library Load Error: ${error.message}"); return false; } }
I am getting following error - Uncaught ReferenceError: exports is not defined
Failed to load NATS.js: Error: nats.js library loaded but nats object not found in global scope.
what URL should I use to load into javascript code? could you please guide me?
Beta Was this translation helpful? Give feedback.
All reactions