From 6ad2f16c24b830b234861c62bf6059786de963c6 Mon Sep 17 00:00:00 2001 From: imw Date: Fri, 28 Dec 2018 12:37:36 +0100 Subject: [PATCH] add ipfs pubsub methods --- data/data.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/data/data.go b/data/data.go index 30a4441..d0a33ac 100644 --- a/data/data.go +++ b/data/data.go @@ -8,7 +8,7 @@ import ( shell "github.com/ipfs/go-ipfs-api" ) -func publish(object []byte) (string) { +func publish(object []byte) string { sh := shell.NewShell("localhost:5001") cid, err := sh.Add(bytes.NewBuffer(object)) if err != nil { @@ -28,7 +28,7 @@ func pin(path string) { } -func retrieve(hash string) ([]byte){ +func retrieve(hash string) []byte { sh := shell.NewShell("localhost:5001") reader, err := sh.Cat(hash) if err != nil { @@ -42,3 +42,22 @@ func retrieve(hash string) ([]byte){ } return content } + +func PsSubscribe(topic string) *shell.PubSubSubscription { + sh := shell.NewShell("localhost:5001") + sub, err := sh.PubSubSubscribe(topic) + if err != nil { + fmt.Fprintf(os.Stderr, "error: %s", err) + os.Exit(1) + } + return sub +} + +func PsPublish(topic, data string) { + sh := shell.NewShell("localhost:5001") + err := sh.PubSubPublish(topic, data) + if err != nil { + fmt.Fprintf(os.Stderr, "error: %s", err) + os.Exit(1) + } +}