npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snet-sdk-shark

v2.0.2

Published

SingularityNET SDK for Nodejs

Downloads

9

Readme

snet-sdk

npm

SingularityNET SDK for Node.js

Getting Started

These instructions are for the development and use of the SingularityNET SDK for JavaScript on Node.js platform.

Installation

npm install snet-sdk

Usage

The SingularityNET SDK allows you to import compiled client libraries for your service or services of choice and make calls to those services programmatically from your application by setting up state channels with the providers of those services and making gRPC calls to the SingularityNET daemons for those services by selecting a channel with sufficient funding and supplying the appropriate metadata for authentication.

import SnetSDK from 'snet-sdk';
import config from './config';
const sdk = new SnetSDK(config);

You can find a sample config below

{
  "web3Provider": "https://ropsten.infura.io/v3/1234567890",
  "privateKey": "",
  "signerPrivateKey": "",
  "networkId": "3",
  "ipfsEndpoint": "http://ipfs.organization.io:80",
  "defaultGasPrice": "4700000",
  "defaultGasLimit": "210000"
}

Now, the instance of the sdk can be used to instantiate clients for SingularityNET services. To interact with those services, the sdk needs to be supplied with the compiled gRPC client libraries.

To generate the gRPC client libraries, you need the SingularityNET Command Line Interface, or CLI, which you can download from PyPi, see https://github.com/singnet/snet-cli#installing-with-pip

Once you have the CLI installed, run the following command:

$ snet sdk generate-client-library nodejs <org_id> <service_id>

Optionally, you can specify an output path; otherwise it's going to be ./client_libraries/nodejs/<hash>/<org_id>/<service_id> Once you have the generated gRPC client libraries, you can create an instance of a SingularityNET service client:

import services from '<path_to_grpc_service_file>'
import messages from '<path_to_grpc_message_file>'
const client = sdk.createServiceClient("<org_id>", "<service_id>", "<services.<ClientStub>>")

This generates a service client which can be used to make gRPC calls to the desired service. You can then invoke service specific calls as follows

client.service.<methodName>(<gRPC.message>, callback);

Concurrency

SDK exposes two methods to facilitate concurrent service calls.

  • getConcurrencyTokenAndChannelId
  • setConcurrencyTokenAndChannelId

In the consumer, you should call the getConcurrencyTokenAndChannelId() in the master thread.
It will return the concurrency token and the channel id. Pass both of them to worker threads and the set the same in the respective instances using setConcurrencyTokenAndChannelId.

SDK also exposes the class DefaultPaymentStrategy to handle the payment metadata for concurrent calls. Initialize the DefaultPaymentStrategy with the number of calls you would want to run concurrently.

e.g

import SnetSDK, { DefaultPaymentStrategy } from "snet-sdk";
const sdk = new SnetSDK(config);
import cluster from "cluster";

const main = async () => {
...
// Planning for four concurrent calls
const paymentStrategy = new DefaultPaymentStrategy(4);
const serviceClient = await sdk.createServiceClient(
       orgId,
       serviceId,
       service.CalculatorClient,
       groupName,
       paymentStrategy,
       serviceClientOptionsFreeCall
     );

if(cluster.isMaster) {
const {concurrencyToken, channelId} = await serviceClient.getConcurrencyTokenAndChannelId()
const worker = cluster.fork()
worker.on("message", message=>{
    console.log(`worker:${worker.id}, message:${message}`)
    worker.send({concurrencyToken,channelId, info:"master: sent you the concurrency token and channel id"})
})
}else {
process.send(`send me the token for concurrency`);
process.on("message", async (message) => {
        const { concurrencyToken, info,channelId } = message;
        console.log(info);
        serviceClient.setConcurrencyTokenAndChannelId(concurrencyToken,channelId)
        const numbers = new messages.Numbers();
        numbers.setA(6);
        numbers.setB(7);
        serviceClient.service.mul(numbers, (err, result)=>{
         if(err) {
           console.error(`service failed with error ${err}`)
         }else{
           console.log(`service response is ${result}`)
           }
        });
});
}
main()

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.