19

I don't know how to get a javascript file to work for web-browser functionalit, when it's coded as node.js.

The code in question is from a github graphenejs-lib. I want to convert this node.js code into js:

import {Apis} from "graphenejs-ws";
var {ChainStore} = require("graphenejs-lib");
Apis.instance("wss://bitshares.openledger.info/ws", true).init_promise.then((res) => {
    console.log("connected to:", res[0].network);
    ChainStore.init().then(() => {
        ChainStore.subscribe(updateState);
    });
});
let dynamicGlobal = null;
function updateState(object) {
    dynamicGlobal = ChainStore.getObject("2.1.0");
    console.log("ChainStore object update\n", dynamicGlobal ? dynamicGlobal.toJS() : dynamicGlobal);
}

There is another github from the same developer, steemjs-lib, that shows both the node.js use, and the browser use in the README default page.

I don't know how to make graphenejs-lib into browser javascript, like the steemjs-lib has been made into a regular javascript working version. I contact the dev, but have yet to receive a response.

I figured other people actually know how to do what the dev did for steemjs-lib, and get the graphenejs-lib to work in a browser.

Can you help me out? Thank you.

1
  • I created a jsfiddle which generates an error for the Api when I added the file https://wzrd.in/standalone/graphenejs-ws@latest and code var {Apis} = graphenejsWs;, that doesn't seem to be the right call: Uncaught (in promise) Error: Api not found jsfiddle.net/rhwmgLta Commented Jan 18, 2017 at 19:56

2 Answers 2

9

You can use Browserify to help you with that:

Note that not all of the Node's APIs will be available in the browser.

Sign up to request clarification or add additional context in comments.

Comments

7

Use browserify. https://wzrd.in/ will package it for you if you just want to use that one library. https://wzrd.in/standalone/graphenejs-lib@latest

<script src="https://wzrd.in/standalone/graphenejs-lib@latest"></script>
<script>
  const {ChainStore} = graphenejsLib;
  ...
</script>

4 Comments

Thanks. What about the import {Apis} from "graphenejs-ws"; ?
I created a jsfiddle which generates an error for the Api when I added the file https://wzrd.in/standalone/graphenejs-ws@latest and code var {Apis} = graphenejsWs;, that doesn't seem to be the right call: Uncaught (in promise) Error: Api not found jsfiddle.net/rhwmgLta
You probably want to use a version already built at github.com/svk31/steemjs-lib/tree/master/build. Try using rawgit.com/svk31/steemjs-lib/master/build/steemjs-lib.js in JSFiddle.
"The code in question is from a github graphenejs-lib. I want to convert this node.js code into js..." from the OP. Not for steemjs. The OP also shows the code in question, that is in node.js format, and I'm asking for regular JS. Here is the graphenejs-lib.js file: github.com/svk31/graphenejs-lib/tree/master/build

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.