1.0.3 • Published 4 years ago

message-peer-connection v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

WebRTC Peer connection abstraction for messages exchanges.

Installation

$ yarn add message-peer-connection

or

$ npm i message-peer-connection

Usage

import PeerConnection from 'message-peer-connection'

const onClose = () => console.log('Connecion Closed')
const onCreateOffer = (event, peerConnection) => sendOfferToPeer(peerConnection)
const onOpen = () => console.log('Connection Stablished')
const onSetOffer = (event, peerConnection) => sendAnswerToPeer(peerConnection)

const peerConnection = PeerConnection({
  onOpen,
  onClose,
  onCreateOffer,
  onSetOffer,
})

APIs

PeerConnection

PeerConnection({
    pc,
    onClose,
    onCreateOffer,
    onError,
    onMessage,
    onOpen,
    onSetAnswer,
    onSetOffer,
    ...state,
})
ParamMeaning
pcRTCPeerConnection instance, default value is new RTCPeerConnection({ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] }).
onCloseonClose.
onCreateOfferonCreateOffer.
onErroronError.
onMessageonMessage.
onOpenonOpen.
onSetOfferonSetOffer.
...stateAny state data.
FunctionMeaningSpecification
closeCloses the connection. Close abstraction.() -> PeerConnection.
createOfferRTCPeerConnection createOffer abstraction() -> PeerConnection
getLocalDescriptionSDPRetuns the RTCPeerConnection's localDescription's SDP() -> SDP
getStateReturns the PeerConnection state.() -> Object
sendMessageAdds a title prop to the data object, stringifies it and calls sendPlainTextMessage.(title: string, data: object) -> PeerConnection
sendPlainTextMessagechannel abstractionmessage: string -> PeerConnection
setAnswersetRemoteDescription abstraction that sets an RTCSessionDescription answer.answer: string -> PeerConnection
setOffersetRemoteDescription abstraction that sets an RTCSessionDescription offer and creates an answer.string -> PeerConnection
setPCSets the RTCPeerConnection instance that will be usedpc: RTCPeerConnection -> PeerConnection
setStateAdds new state values.newStateValues: object -> PeerConnection
updateOnCloseUpdates the onClose listeneronClose: function -> PeerConnection
updateOnCreateOfferUpdates the onCreateOffer listeneronCreateOffer: function -> PeerConnection
updateOnErrorUpdates the onError listeneronError: function -> PeerConnection
updateOnMessageUpdates the onMessage listeneronMessage: function -> PeerConnection
updateOnOpenUpdates the onOpen listeneronOpen: function -> PeerConnection
updateOnSetOfferUpdates the onSetOffer listeneronSetOffer: function -> PeerConnection

Listeners

onClose

Listener function executed when the connection closes, it receives two arguments oniceconnectionstatechange Event and the PeerConnection instance.

onCreateOffer

Listener function executed when an offer is created, it receives two arguments onicecandidate Event and the PeerConnection instance.

onError

Listener function executed when an error is catch while trying to create or set an offer or set an answer it receives two arguments Event and the PeerConnection instance.

onMessage

Listener function executed when a message is receive, it receives two arguments onmessage Event and the PeerConnection instance.

onOpen

Listener function executed when the connection is stablished, it receives two arguments onopen Event and the PeerConnection instance.

onSetOffer

Listener function executed when an offer is set, it receives two arguments onicecandidate Event and the PeerConnection instance.

TODO

  • Readme
  • Example folder
  • Tests

References

https://www.html5rocks.com/en/tutorials/webrtc/infrastructure/