Signal Fire Connect

Signal Fire Connect

I’m happy to introduce Signal Fire Connect, a very slim client for Signal Fire Server. Connect provides a minimal interface to make working with the Server a breeze. In contrast to Signal Fire Client it ditches the sessions in favor of a more low-level approach. This makes Connect more relevant to varying use cases, and allows developers more freedom in how to handle WebRTC in their web applications.

So, what does Connect actually do? It does the minimum necessary for a signaling server interface; it allows sending and receiving of offers, answers, and ICE candidates. What the developer does with them afterward is of no concern to the module.

Below we find an example using Signal Fire Peer, a wrapper around the native RTCPeerConnection which makes life a little easier while still remaining low-level.

import Connect, { ConnectInit, IncomingOfferEvent, IncomingAnswerEvent, IncomingICECanidateEvent } from './index'
import Peer, { OfferEvent, AnswerEvent, ICECandidateEvent } from '@signal-fire/peer'

// Initial configuration
const init: ConnectInit {
  reconnectOnClose: false,
  reconnectOnError: true,
  reconnectInterval: 2000,
  reconnectAttempts: 2,
  urlTransform: previousUrl => previousUrl
}

// Create a new Connect instance
const connect = new Connect(init)

// Connect to the signaling server
await connect.connect('wss://rtc.example.com')

// Create a new peer
const target = '<target id>'
const connection = new RTCPeerConnection()
const peer = new Peer(connection)

peer.addEventListener('offer', ({ detail: offer }: OfferEvent) => {
  // send the offer to the remote peer through the signaling server
  connect.sendOffer(target, offer).catch((err: Error) => { /* ... */ })
})

peer.addEventListener('answer', ({ detail: answer }: AnswerEvent) => {
  // send the answer to the remote peer through the signaling server
  connect.sendAnswer(target, answer).catch((err: Error) => { /* ... */ })
})

peer.addEventListener('icecandidate', ({ candidate }: ICECandidateEvent) => {
  if (candidate) {
    // send the ICE candidate to the remote peer through the signaling server
    connect.sendICECandidate(target, candidate).catch((err: Error) => { /* ... */ })
  }
})

// Listen to incoming offers
connect.addEventListener('offer', ({ detail: { origin, offer } }: IncomingOfferEvent) => {
  if (origin === target) {
    peer.handleIncomingOffer(offer)
  }
})

// Listen to incomging answers
connect.addEventListener('answer', ({ detail: { origin, answer } }: IncomingAnswerEvent) => {
  if (origin === target) {
    peer.handleIncomingAnswer(offer)
  }
})

// Listen to incoming ICE candidates
connect.addEventListener('ice', ({ detail: { origin, candidate } }: IncomingICECandidateEvent) => {
  if (origin === target) {
    peer.handleIncomingICECandidate(candidate)
  }
})

As you can see, only the bare minimum is being handled here. Connect provides this low-level interface to a Signal Fire Server instance.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.