-
Notifications
You must be signed in to change notification settings - Fork 1.2k
FloodSub API discussion #530
Description
js-ipfs should expose the (IPFS) pubsub API.
API
UPDATED on 10th November 2016 by @haadcode
Subscribe
ipfs.pubsub.subscribe(topic, options, callback)
topic is type of string.
In the future, topic can also be type of TopicDescriptor (https://github.com/libp2p/pubsub-notes/blob/master/flooding/flooding.proto#L23). However, for now, only strings are supported.
options is an object with subscription options.
Currently the options object is empty, ie. no options are supported, but in the future we'll add any options that can be passed into this argument.
callback signature is (err, stream) where stream is a Stream. stream emits data event upon new message to the specified topic.
If no callback is passed, returns a Promise that resolves to a Stream:
ipfs.pubsub.subscribe(topic, options).then((stream) => ...)
Unsubscribe
ipfs.pubsub.unsubscribe(topic)
topic is type of string.
Publish
ipfs.pubsub.publish(topic, data)
topic is type of string.
data can be a Buffer or anything that converts to a buffer with new Buffer(data).
Usage
ipfs.pubsub.sub('hello', (err, stream) => {
stream.on('data', (message) => {
console.log(message)
stream.cancel() // cancel subscription
/* {
data: 'world',
topicIDs: ['hello']
// these fields are currently missing from the message
// (but are present in messages from go-ipfs pubsub)
// from: bs58.encode(message.from),
// seqno: Base64.decode(message.seqno)
} */
})
})
ipfs.pubsub.sub('hello', 'world')