Giter Club home page Giter Club logo

unicast-definition's Introduction

unicast-definition Build Status

Keywords: Unicast, browser-to-browser communication, WebRTC

Unicast component relying on a n2n component. Similarly to socket.io, this module provides an event-like API to send and receive messages.

Installation

npm install unicast-definition

API

The API is available here.

Usage

// require as you want the N2N module
const S = require('n2n-overlay-wrtc')
// require this module
const U = require('unicast-definition')

// #1 create 3 peers
const s1 = new S({peer: '1', config: {trickle: true}})
const s2 = new S({peer: '2', config: {trickle: true}})
const s3 = new S({peer: '3', config: {trickle: true}})
// #2 associate a unicast protocol to each peer
const u1 = new U(s1, {retry: 1, pid: 'com1'})
const u2 = new U(s2, {retry: 1, pid: 'com1'})
const u3 = new U(s3, {retry: 1, pid: 'com1'})

u1.on('meow', (from, i, am, a, cat) => console.log('@s1 from %s: %s %s %s %s', from, i, am, a, cat))
u2.on('meow', (from, i, am, a, cat) => console.log('@s2 from %s: %s %s %s %s', from, i, am, a, cat))
u3.on('meow', (from, i, am, a, cat) => console.log('@s3 from %s: %s %s %s %s', from, i, am, a, cat))

const u4 = new U(s1, {retry: 1, pid: 'com2'})
const u5 = new U(s2, {retry: 1, pid: 'com2'})
const u6 = new U(s3, {retry: 1, pid: 'com2'})

u4.on(':3', (from, i, is, cat) => console.log('@s1: miaw'))
u5.on(':3', (from, i, is, cat) => console.log('@s2: meow'))
u6.on(':3', (from, i, is, cat) => console.log('@s3: miou'))

// #4 simulate signaling server
const callback = (from, to) => {
  return (offer) => {
    to.connect((answer) => { from.connect(answer) }, offer)
  }
}

// #4 s1 contacts s2, 2-peers network
s1.connection(s2).then(() => {
  console.log('s1 <=> s2.')
  s3.connection(s2).then(() => {
    console.log('s1 <=> s2; s1 -> s3; s3 -> s2'))
    u3.emit('meow', s2.getOutviewId(), 'i', 'am', 'a', 'cat').then(() => {
      u3.emit(':3', s2.getOutviewId(), 'parameters', 'dont', 'matter')).then(() => {
        u6.emit(':3', s2.getOutviewId(), 'parameters', 'dont', 'matter')
      })
    })
  })
})

unicast-definition's People

Contributors

chat-wane avatar folkvir avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

nhaouari

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.