pubsub.go 1.17 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4 5 6
package iface

import (
	"context"
	"io"

Łukasz Magiera's avatar
Łukasz Magiera committed
7
	options "github.com/ipfs/interface-go-ipfs-core/options"
Łukasz Magiera's avatar
Łukasz Magiera committed
8

Łukasz Magiera's avatar
Łukasz Magiera committed
9
	peer "github.com/libp2p/go-libp2p-peer"
Łukasz Magiera's avatar
Łukasz Magiera committed
10 11 12 13 14 15
)

// PubSubSubscription is an active PubSub subscription
type PubSubSubscription interface {
	io.Closer

16 17
	// Next return the next incoming message
	Next(context.Context) (PubSubMessage, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
18 19 20 21 22 23 24 25 26
}

// PubSubMessage is a single PubSub message
type PubSubMessage interface {
	// From returns id of a peer from which the message has arrived
	From() peer.ID

	// Data returns the message body
	Data() []byte
27 28 29 30 31 32

	// Seq returns message identifier
	Seq() []byte

	// Topics returns list of topics this message was set to
	Topics() []string
Łukasz Magiera's avatar
Łukasz Magiera committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
}

// PubSubAPI specifies the interface to PubSub
type PubSubAPI interface {
	// Ls lists subscribed topics by name
	Ls(context.Context) ([]string, error)

	// Peers list peers we are currently pubsubbing with
	Peers(context.Context, ...options.PubSubPeersOption) ([]peer.ID, error)

	// Publish a message to a given pubsub topic
	Publish(context.Context, string, []byte) error

	// Subscribe to messages on a given topic
47
	Subscribe(context.Context, string, ...options.PubSubSubscribeOption) (PubSubSubscription, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
48
}