Giter VIP home page Giter VIP logo

peersockets's Introduction

peersockets

Build Status

Warning: Extension messages are currently not MAC'd, so we can't detect tampering. This is still experimental!

Peersockets is a lightweight wrapper around @corestore/networker that lets you exchange topic-tagged messages with peers over hypercore-protocol streams.

To use a Peersocket, you first join a topic -- under the hood, this will register a topic-specific extension on every current peer connection in your networker. By defining topics at the extension level, Peersockets does not need to do any extra encoding/decoding. Once you've created a topic, you can send messages to peers by their NOISE keys. If the remote peer has also joined the topic, they will receive the message.

Upgrading

If you've already been using peersockets v0, take a look at UPGRADE.md to see what's changed in v1.

Installation

npm i peersockets --save

Example

This example assumes you already have Networker (@corestore/networker) instance.

const networker = (my swarm networker)
const sockets = new Peersockets(networker)

// Let's say you have a friend's Peer object (which can be extracted from the Network's `peers` property or `peer-add` event).
const friendPeer = networker.peers[0]

const handle = sockets.join('my-topic', {
  onmessage: (msg, peer) => {
   console.log(`I got message ${msg} from ${peer.remotePublicKey.toString('hex')}`)
  }
})

// If your friend is also subscribed to topic 'my-topic', they will receive this message.
handle.send('hello!', friendPeer)

API

const socket = new Peersockets(networker)

Creates a new Peersockets instance.

const handler = socket.join(topicName, opts = { onmessage, onclose })

Joins a new topic. If this is the first time the topic has been joined, a topic extension will be registered on every connected peer. Otherwise the previous topic/extension will be reused.

  • topicName can be any string
  • opts can contain the following callbacks:
    • onmessage(msg, peer) - Called whenever a message with this topic is received from peer.
    • onclose() - Called if the topic is closed via leave.

The topic handler's API is described below.

socket.leave([topicName])

Close all topic handlers for topicName and unregister the topic's extension from all connected peers.

If a topicName is not specified, all topics will be left.

topicHandle.send(msg, peer)

Send a message to peer.

msg is a string or a Buffer. peer is a @corestore/networker Peer instance that can be obtained from networker.peers.

topicHandle.close()

Close the handle when you're finished with it. This will not unregister the topic extension.

License

MIT

peersockets's People

Contributors

andrewosh avatar

Stargazers

jiangplus avatar monz avatar Doug A avatar leo avatar Etienne Martin avatar Peter Seprus avatar Francis Chong avatar Tejesh Mehta avatar nichoth avatar  avatar Franz Heinzmann avatar Andrew Chou avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.