0.1.17 • Published 1 year ago

@dxos/broadcast v0.1.17

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Broadcast

Abstract module to send broadcast messages.

Allows a node to originate a message that will be received at least once, within a reasonably short time, on all nodes that are reachable from the origin node. Messages are propagated via the middleware specified. Broadcast storms are avoided by means of a flooding routing scheme.

Broadcast messages follows the schema:

message Packet {
  bytes seqno = 1;
  bytes origin = 2;
  bytes from = 3;
  bytes data = 4;
}
  • seqno: By default is a random 32-bit but could be used to provide an alternative sorted sequence number.
  • origin: Represents the author's ID of the message. To identify a message (msgId) in the network you should check for the: seqno + origin.
  • from: Represents the current sender's ID of the message.
  • data: Represents an opaque blob of data, it can contain any data that the publisher wants it to defined by higher layers (e.g. a presence information message).

Nodes send any message originating locally to all current peers. Upon receiving a message, a node delivers it locally to any listeners, and forward the message on to its current peers, excluding the peer from which it was received.

Nodes maintain a record of the messages they have received and originated recently, by msgId(seqno + from). This is used to avoid sending the same message to the same peer more than once. These records expire after some time to limit memory consumption by: maxAge and maxSize.

Install

$ npm install @dxos/broadcast

Usage

import { Broadcast } from '@dxos/broadcast';

const middleware = {
  subscribe: (onData, updatePeers) => {
    // Defines how to process incoming data and peers update.

    // on('peers', onPeers)
    // on('data', onData)
    return () => {
      // Return a dispose function.
    }
  },
  send: async (packet, node) => {
    // Define how to send your packets.
    // "packet" is the encoded message to send.
    // "node" is the peer object generate from the lookup.
  }
};

const broadcast = new Broadcast(middleware, {
  id: crypto.randomBytes(32),
  maxAge: 15 * 1000, // Timeout for each message in the LRU cache.
  maxSize: 1000 // Limit of messages in the LRU cache.
})

// We initialize the middleware and subscription inside the broadcast.
await broadcast.open()

broadcast.publish(Buffer.from('Hello everyone'))

await broadcast.close()

You can check a real example in: example

API

const broadcast = new Broadcast(middleware, [options])

  • middleware: The middleware defines an interface to connect the broadcast to any request/response solution.

    • subscribe: ({ onData, onPeers }) => unsubscribeFunction: Defines how to subscribe to incoming packets and peers update.
      • onData: (data: Buffer) => (Packet|undefined): Callback to process incoming data. It returns true if the broadcast could decode the message or false if not.
      • onPeers: (peers: [Peer]): Callback to update the internal list of peers. A Peer object must follow the spec: { id: Buffer, ...props }
      • unsubscribeFunction: Function: Defines a way to unsubscribe from listening messages if the broadcast stop working. Helpful if you are working with streams and event emitters.
    • send: (packet: Buffer, peer: Object) => Promise: Defines how to send the packet builded by the broadcast.
  • options

    • id: Buffer: Defines an id for the current peer. Default: crypto.randomBytes(32).
    • maxAge: number: Defines the max live time for the cache messages. Default: 10 * 1000.
    • maxSize: number: Defines the max size for the cache messages. Default: 1000.

broadcast.open() => Promise

Initialize the cache and runs the defined subscription.

broadcast.close() => Promise

Clear the cache and unsubscribe from incoming messages.

broadcast.publish(data, [options]) -> Promise<Packet>

Broadcast a flooding message to the peers neighboors.

  • data: Buffer: Any data that you want to broadcast.
  • options

    • seqno: Buffer: Defines a custom seqno for the message. Default: crypto.randomBytes(32).
  • Packet

    • seqno: Buffer
    • origin: Buffer
    • from: Buffer
    • data: Buffer
0.1.14

1 year ago

0.1.16

1 year ago

0.1.17

1 year ago

0.1.10

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.13

1 year ago

0.1.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.9

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

2.33.1

2 years ago

2.33.0

2 years ago

2.33.7

2 years ago

2.33.6

2 years ago

2.33.8

2 years ago

2.33.3

2 years ago

2.33.2

2 years ago

2.33.5

2 years ago

2.33.4

2 years ago

2.32.0

2 years ago

2.31.1

2 years ago

2.31.0

2 years ago

2.31.3

2 years ago

2.31.2

2 years ago

2.31.5

2 years ago

2.31.4

2 years ago

2.31.7

2 years ago

2.31.6

2 years ago

2.30.2

2 years ago

2.30.1

2 years ago

2.30.0

2 years ago

2.29.0

2 years ago

2.29.1

2 years ago

2.28.22

2 years ago

2.28.23

2 years ago

2.28.20

2 years ago

2.28.21

2 years ago

2.28.19

2 years ago

2.28.17

2 years ago

2.28.18

2 years ago

2.28.15

2 years ago

2.28.16

2 years ago

2.28.9

2 years ago

2.28.8

2 years ago

2.28.5

2 years ago

2.28.4

2 years ago

2.28.7

2 years ago

2.28.6

2 years ago

2.28.13

2 years ago

2.28.14

2 years ago

2.28.11

2 years ago

2.28.12

2 years ago

2.28.10

2 years ago

2.22.0

2 years ago

2.21.0

2 years ago

2.21.2

2 years ago

2.21.1

2 years ago

2.20.0

2 years ago

2.19.8

2 years ago

2.19.9

2 years ago

2.19.6

2 years ago

2.19.7

2 years ago

2.19.4

2 years ago

2.19.5

2 years ago

2.19.3

2 years ago

2.28.1

2 years ago

2.28.0

2 years ago

2.28.3

2 years ago

2.28.2

2 years ago

2.27.9

2 years ago

2.27.6

2 years ago

2.27.5

2 years ago

2.27.8

2 years ago

2.27.7

2 years ago

2.27.2

2 years ago

2.27.1

2 years ago

2.27.4

2 years ago

2.27.3

2 years ago

2.27.0

2 years ago

2.26.1

2 years ago

2.26.0

2 years ago

2.19.10

2 years ago

2.19.11

2 years ago

2.25.0

2 years ago

2.24.0

2 years ago

2.23.0

2 years ago

2.19.2

2 years ago

2.19.0

2 years ago

2.19.1

2 years ago

2.18.3

2 years ago

2.18.2

2 years ago

2.17.4

2 years ago

2.17.2

2 years ago

2.17.3

2 years ago

2.17.0

2 years ago

2.17.1

2 years ago

2.16.9

2 years ago

2.16.7

3 years ago

2.16.8

3 years ago

2.16.6

3 years ago

2.18.1

2 years ago

2.18.0

2 years ago

2.16.15

2 years ago

2.16.14

2 years ago

2.16.17

2 years ago

2.16.16

2 years ago

2.16.11

2 years ago

2.16.10

2 years ago

2.16.13

2 years ago

2.16.12

2 years ago

2.16.5

3 years ago

2.16.3

3 years ago

2.16.4

3 years ago

2.15.8

3 years ago

2.15.9

3 years ago

2.15.6

3 years ago

2.15.7

3 years ago

2.15.4

3 years ago

2.15.5

3 years ago

2.15.2

3 years ago

2.15.3

3 years ago

2.15.0

3 years ago

2.15.1

3 years ago

2.14.5

3 years ago

2.16.1

3 years ago

2.16.2

3 years ago

2.16.0

3 years ago

2.14.3

3 years ago

2.14.4

3 years ago

2.14.1

3 years ago

2.14.2

3 years ago

2.13.0

3 years ago

2.14.0

3 years ago

2.12.23

3 years ago

2.12.22

3 years ago

2.12.21

3 years ago

2.12.20

3 years ago

2.12.19

3 years ago

2.12.18

3 years ago

2.12.17

3 years ago

2.11.0

3 years ago

2.12.0

3 years ago

2.11.1

3 years ago

2.10.10

3 years ago

2.10.11

3 years ago

2.10.12

3 years ago

2.10.13

3 years ago

2.10.14

3 years ago

2.10.15

3 years ago

2.10.16

3 years ago

2.10.17

3 years ago

2.12.9

3 years ago

2.12.7

3 years ago

2.10.9

3 years ago

2.12.16

3 years ago

2.12.8

3 years ago

2.12.15

3 years ago

2.12.5

3 years ago

2.12.14

3 years ago

2.12.6

3 years ago

2.10.8

3 years ago

2.12.13

3 years ago

2.12.3

3 years ago

2.12.12

3 years ago

2.12.4

3 years ago

2.12.11

3 years ago

2.12.1

3 years ago

2.12.10

3 years ago

2.12.2

3 years ago

1.0.0-beta.11

3 years ago

1.0.0-beta.10

4 years ago

1.0.0-beta.9

4 years ago

1.0.0-beta.8

4 years ago

1.0.0-beta.7

4 years ago

1.0.0-beta.6

4 years ago

1.0.0-beta.5

4 years ago

1.0.0-beta.4

4 years ago

1.0.0-beta.3

4 years ago

1.0.0-beta.2

4 years ago

1.0.0-beta.1

4 years ago

1.0.0-beta.0

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago