This package has been deprecated

Author message:

move to @the-/rtc-client ( https://github.com/the-labo/the/tree/master/packages/rtc-client#readme )

the-rtc-client

5.0.10 • Public • Published

the-rtc-client

Build Status npm Version JS Standard

Client for the-rtc

Installation

$ npm install the-rtc-client --save

Usage

Setup Server

'use strict'
 
const theRTC = require('../../the-rtc')
 
// const theRTC = require('the-rtc')
 
async function tryExample() {
  const rtc = theRTC({
    stun: {
      url: 'stun:stun.l.google.com:19302'
    },
    turn: {
      url: 'turn:your.turn.servers.here',
      secret: 'xxxxxxxxxxxxxxxx',
      expiry: 86400
    },
    topology: 'sfu',
  })
 
  const port = 3000
  await rtc.listen(port)
  console.log(`Example RTC Server listening: http://localhost:${port}`)
 
}
 
tryExample().catch((err) => console.error(err))
 

Create Clients

'use strict'
 
const { TheRTCClient } = require('../lib')
 
document.addEventListener('DOMContentLoaded', async () => {
  const url = 'http://localhost:3000'
 
  const video01 = document.getElementById('video01')
  const video02 = document.getElementById('video02')
  const video03 = document.getElementById('video03')
  const video04 = document.getElementById('video04')
  const video01Title = document.getElementById('video01-title')
  const video02Title = document.getElementById('video02-title')
  const video03Title = document.getElementById('video03-title')
  const video04Title = document.getElementById('video04-title')
 
  const c1 = new TheRTCClient({
    info: { userName: 'This is client01' },
    mediaConstrains: { audio: false, video: true },
    onLocal: ({ stream, info, rid }) => {
      video01Title.innerText = info.userName
      setVideoStream(video01, stream)
    },
    onRemote: ({ stream, info, rid }) => {
      video02Title.innerText = info.userName
      setVideoStream(video02, stream)
    }
  })
  const c2 = new TheRTCClient({
    info: { userName: 'This is client02' },
    mediaConstrains: { audio: false, video: true },
    onLocal: ({ stream, info, rid }) => {
      video03Title.innerText = info.userName
      setVideoStream(video03, stream)
    },
    onRemote: ({ stream, info, rid, peer }) => {
      video04Title.innerText = info.userName
      setVideoStream(video04, stream)
    }
  })
 
  await c1.connect(url)
  await c2.connect(url)
 
  const roomName = 'room01'
 
  const setVideoStream = (video, stream) => {
    if (!stream) {
      console.warn(`No stream`)
      return
    }
    const knownId = video.srcObject && video.srcObject.id
    if (knownId === stream.id) {
      return
    }
    video.srcObject = stream
  }
 
  await c1.join(roomName)
  await c2.join(roomName)
 
  {
    const unsubscribe = c2.subscribe('greeting', ({ from, payload }) => {
      console.log('c2 received greeting', from, payload)
      unsubscribe()
    })
  }
 
  {
    const unsubscribe = c1.subscribe('greeting', ({ from, payload }) => {
      console.log('c1 received greeting', from, payload)
      unsubscribe()
    })
  }
 
  setTimeout(() => {
    void c1.publish('greeting', { msg: 'hi, i am c1' })
    void c2.publish('greeting', { msg: 'hi, i am c2' })
  }, 500)
})
 

API Guide

License

This software is released under the MIT License.

Links

Readme

Keywords

Package Sidebar

Install

npm i the-rtc-client

Weekly Downloads

1

Version

5.0.10

License

MIT

Unpacked Size

1.29 MB

Total Files

95

Last publish

Collaborators

  • okunishinishi