Possible to use go-ipfs as a lib?

I am exploring the possibility of embedding go-ipfs in another application I have, where I programmatically create a node and programmatically perform tasks on that node…I looked briefly into the go-ipfs on github and it was not immediately clear if I can use it as a library instead of as a binary.

I also see no example on how to go about this. (Most or all examples are focused on running the go-ipfs binary instead)

Anyone has pointers on how to achieve my desire?

1 Like

Link to go-ipfs-as-a-library example:

Also, depending on your requirements: https://github.com/hsanjuan/ipfs-lite

2 Likes

Thanks @hector

So I got around to start playing with this. I was able to follow the example and it works fine.

Now I am trying to see if I can run the node but with pubsub support enabled. I followed the link here https://github.com/ipfs/go-ipfs/tree/master/docs/examples/library-experimental-features but it seems there is no “pubsub” upon on the Experiment struct that can be enabled.

type Experiments struct {
	FilestoreEnabled     bool
	UrlstoreEnabled      bool
	ShardingEnabled      bool
	GraphsyncEnabled     bool
	Libp2pStreamMounting bool
	P2pHttpProxy         bool
	QUIC                 bool
	StrategicProviding   bool
}

or is there another means of getting PubSub enabled programmatically?

The Core node gives you access to a Pubsub: https://godoc.org/github.com/ipfs/go-ipfs/core#IpfsNode

Note that the example uses a default configuration: https://github.com/ipfs/go-ipfs/blob/master/docs/examples/go-ipfs-as-a-library/main.go#L56 and you may want to change things on that configuration before creating the node.

Thanks for the response @hector

This is exactly what I wanted to do. If you check the link I initially linked to https://github.com/ipfs/go-ipfs/tree/master/docs/examples/library-experimental-features, it shows how to change the configurations

// Create a config with default options and a 2048 bit key
	cfg, err := config.Init(ioutil.Discard, 2048)
	if err != nil {
		return "", err
	}

// Here, configuration is being updated	

	// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-filestore
	cfg.Experimental.FilestoreEnabled = true
	// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-urlstore
	cfg.Experimental.UrlstoreEnabled = true
	// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#directory-sharding--hamt
	cfg.Experimental.ShardingEnabled = true
	// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-p2p
	cfg.Experimental.Libp2pStreamMounting = true
	// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#p2p-http-proxy
	cfg.Experimental.P2pHttpProxy = true
	// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#strategic-providing
	cfg.Experimental.StrategicProviding = true

But the problem now is, I do not see how to enable pubsub, using this pattern? Nothing like cfg.Experimental.PubSub = true is available. That is the part I am stranded at.

Right, but is the Pubsub field in core.IpfsNode nil?

I will check that out in a bit…

I confirmed it is nil

I had a break point somewhere around here and I confirmed it is nil.

There is a field in the config to enable it: https://godoc.org/github.com/ipfs/go-ipfs-config#PubsubConfig