message-peer-connection
v1.0.3
Published
WebRTC Peer Connection abstraction for messages exchanges.
Downloads
7
Maintainers
Readme
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,
})
Param | Meaning
---|---
pc
| RTCPeerConnection instance, default value is new RTCPeerConnection({ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] })
.
onClose
| onClose.
onCreateOffer
| onCreateOffer.
onError
| onError.
onMessage
| onMessage.
onOpen
| onOpen.
onSetOffer
| onSetOffer.
...state
| Any state data.
Function | Meaning | Specification
---|---|---
close
| Closes the connection. Close abstraction. | () -> PeerConnection.
createOffer
| RTCPeerConnection createOffer abstraction | () -> PeerConnection
getLocalDescriptionSDP
| Retuns the RTCPeerConnection's localDescription's SDP | () -> SDP
getState
| Returns the PeerConnection state. | () -> Object
sendMessage
| Adds a title prop to the data object, stringifies it and calls sendPlainTextMessage
. | (title: string, data: object) -> PeerConnection
sendPlainTextMessage
| channel abstraction | message: string -> PeerConnection
setAnswer
| setRemoteDescription abstraction that sets an RTCSessionDescription answer. | answer: string -> PeerConnection
setOffer
| setRemoteDescription abstraction that sets an RTCSessionDescription offer and creates an answer. | string -> PeerConnection
setPC
| Sets the RTCPeerConnection instance that will be used | pc: RTCPeerConnection -> PeerConnection
setState
| Adds new state values. | newStateValues: object -> PeerConnection
updateOnClose
| Updates the onClose listener | onClose: function -> PeerConnection
updateOnCreateOffer
| Updates the onCreateOffer listener | onCreateOffer: function -> PeerConnection
updateOnError
| Updates the onError listener | onError: function -> PeerConnection
updateOnMessage
| Updates the onMessage listener | onMessage: function -> PeerConnection
updateOnOpen
| Updates the onOpen listener | onOpen: function -> PeerConnection
updateOnSetOffer
| Updates the onSetOffer listener | onSetOffer: 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
- [x] Readme
- [ ] Example folder
- [ ] Tests
References
https://www.html5rocks.com/en/tutorials/webrtc/infrastructure/