{"id":21540314,"url":"https://github.com/lostsource/simplertcdata","last_synced_at":"2025-10-12T21:44:15.685Z","repository":{"id":29898856,"uuid":"33444512","full_name":"lostsource/SimpleRTCData","owner":"lostsource","description":"Browser-to-Browser Messaging Simplified","archived":false,"fork":false,"pushed_at":"2017-08-05T17:11:27.000Z","size":387,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T14:46:57.953Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lostsource.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-05T15:05:26.000Z","updated_at":"2017-07-23T19:48:17.000Z","dependencies_parsed_at":"2022-08-27T02:30:53.068Z","dependency_job_id":null,"html_url":"https://github.com/lostsource/SimpleRTCData","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostsource%2FSimpleRTCData","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostsource%2FSimpleRTCData/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostsource%2FSimpleRTCData/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostsource%2FSimpleRTCData/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lostsource","download_url":"https://codeload.github.com/lostsource/SimpleRTCData/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248151541,"owners_count":21056129,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T04:18:03.715Z","updated_at":"2025-10-12T21:44:10.651Z","avatar_url":"https://github.com/lostsource.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [![SimpleRTCData](http://i.imgur.com/BQpkqkE.png)](#)\n\nSimpleRTCData is a tiny JavaScript library which can be used to establish an RTCDataChannel between two peers. It does not handle relaying of messages during connection setup so a separate signalling mechanism is required.\n\n- [How it works](#how-it-works)\n- [Basic Usage](#basic-usage)\n- [Constructor](#constructor)\n- [Methods](#methods)\n\t- [close](#close)\n\t- [getAnswer](#getanswer)\n\t- [getConnection](#getconnection)\n\t- [getDataChannel](#getdatachannel)\n\t- [getOffer](#getoffer)\n\t- [request](#request)\n\t- [send](#send)\n\t- [setAnswer](#setanswer)\n- [Events](#events)\n  - [on('connect')](#onconnect)\n  - [on('data')](#ondata)\n  - [on('disconnect')](#ondisconnect)\n  - [on('request')](#onrequest)\n  - [onChannelEvent](#onchannelevent)\n  - [onConnectionEvent](#onconnectionevent)\n\n## How it works\n\n1. Peer A calls `getOffer` and sends the offer to Peer B\n2. Peer B calls `getAnswer` and sends the answer to Peer A\n3. Peer A calls `setAnswer` using the answer received from Peer B\n\n*You may want to go through these steps using this [online example](https://lostsource.github.io/SimpleRTCData/)*\n\n[![SimpleRTCData Signalling Example](http://i.imgur.com/jljAcGQ.png)](https://lostsource.github.io/SimpleRTCData/)\n\n## Basic Usage\n\nStart by including the library in your markup,\n\n    \u003cscript type='text/javascript' src='SimpleRTCData.js\u003e\u003c/script\u003e\n\nAssume we have two peers Bert and Ernie in which Bert is the initiator. Bert first needs to make an 'offer' to Ernie by calling the `getOffer` method:\n\n    var BertRTC = new SimpleRTCData;\n    BertRTC.getOffer(function(bertsOffer) {\n      \n    });\n\nThe callback for `getOffer` returns a single argument `bertsOffer` of type `String` which needs to be sent to Ernie using your preferred message exchange mechanism (eg. WebSockets) Once Ernie receives Bert's offer he will need to create an 'answer' by passing it as the first argument to `getAnswer`. \n\n    var ErnieRTC = new SimpleRTCData;\n     \n    ErnieRTC.getAnswer(bertsOffer,function(erniesAnswer) {\n      // now send erniesAnswer Bert\n    });\n    \nHe should also listen for messages from Bert using the `on('data')` event: \n\n    ErnieRTC.on('data',function(data) {\n       // get ready for messages from Bert \n       console.log(data);\n    });\n     \n    \nFinally, send `erniesAnswer` to Bert so he can use it to call his `setAnswer` method:\n\n    BertRTC.on('connect',function(){\n       // Bert's ready to send messages\n       BertRTC.send('Hey Ernie!');\n    });\n\n    BertRTC.setAnswer(erniesAnswer);\n\n## Constructor\n*SimpleRTCData([RTCConfiguration], [MediaConstraints], [RTCDataChannelInit])*\n\nCreating a `SimpleRTCData` instance does not require any arguments. However it is possible to customize options for the underlying `RTCPeerConnection` and `RTCDataChannel` by passing standard [RTCConfiguration](https://developer.mozilla.org/en-US/docs/Web/API/RTCConfiguration), [MediaConstraints](https://www.webrtc-experiment.com/docs/WebRTC-PeerConnection.html) and [RTCDataChannelInit](http://html5index.org/WebRTC%20-%20RTCDataChannelInit.html) options.\n\n## Methods\n\n### close\n*SimpleRTCData.close(void)*\n\nCloses the current connection. The [`disconnect`](#ondisconnect) event will be triggered on both local and remote peers.\n\n### getAnswer\n*SimpleRTCData.getAnswer(String offer, Function callback)*\n\nThe *joiner* should call this method after receiving an offer from the *initiator*. The offer should be passed as the first argument. The callback function gets one argument `(String answer)`\n\n### getConnection\n*SimpleRTCData.getConnection(void)*\n\nReturns a reference to the session's [RTCPeerConnection](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection) instance\n\n### getDataChannel\n*SimpleRTCData.getDataChannel(void)*\n\nReturns a reference to the session's [RTCDataChannel](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel) instance\n\n\n### getOffer\n*SimpleRTCData.getOffer(Function callback)*\n\nThe *initiator* must call this method to retrieve the offer metadata which should be used by the *joiner* as the first argument for the `getAnswer` method. The callback function gets one argument `(String offer)`.\n\n### request\n*SimpleRTCData.request(String requestId, Function callback)*\n\nRequests data from peer triggering the [request event](#onrequest) on the remote peer. Data sent by the peer will be passed to the callback function.\n\n### send\n*SimpleRTCData.send(DOMString | ArrayBuffer message, [Function callback])*\n\nSends `message` of type *DOMString* or *ArrayBuffer* to peer. An optional callback can be passed as a second argument which will be triggered upon receipt of confirmation from the remote peer.\n\n### setAnswer\n*SimpleRTCData.setAnswer(String answer, [Function callback])*\n\nThe *initiator* must call this method after receiving an answer from the *joiner*. The answer should be passed as the first argument. The second optional argument can be used to check if the call was successful.\n\n\n## Events\n\n### on('connect')\n*SimpleRTCData.on('connect', Function callback)*\n\nEmitted when [RTCPeerConnection.iceConnectionState](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState) has a value of `completed` and [RTCDataChannel.readyState](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/readyState) is `open`. At this point peers can start exchanging messages.\n\n### on('data')\n*SimpleRTCData.on('data', Function callback)*\n\nEmitted when a message event is received on the [RTCDataChannel](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/onmessage). The callback receives one argument `data` containing the message payload.\n\n### on('disconnect')\n*SimpleRTCData.on('disconnect', Function callback)*\n\nEmitted when [RTCPeerConnection.iceConnectionState](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState) has a value of `disconnected` or the [RTCDataChannel.onclose](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/onclose) event is triggered.\n\n### on('request')\n*SimpleRTCData.on('request', Object SimpleRTCDataRequest)*\n\nEmitted when the remote peer calls the [request] method. The callback contains a SimpleRTCDataRequest object which can be used to send a reply to the requesting peer.\n\n### onChannelEvent\n*SimpleRTCData.onChannelEvent(String eventType, Function callback)*\n\nThis handler forwards events of type `eventType` from the [RTCDataChannel](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel) instance to the callback specified in the second argument. \n\nThe `eventType` parameter also accepts the special '*' value which forwards all known `RTCDataChannel` events to a single handler.\n\n### onConnectionEvent\n*SimpleRTCData.onConnectionEvent(String eventType, Function callback)*\n\nThis handler forwards events of type `eventType` from the [RTCPeerConnection](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection) instance to the callback specified in the second argument. \n\nFor Example:\n\n    var RTC = new SimpleRTCData;\n    RTC.onConnectionEvent('icecandidate',myCandidateHandler);\n\nIs identical to:\n\n    var RTC = new SimpleRTCData;\n    var connection = RTC.getConnection(); // returns the standard RTCPeerConnection\n    connection.onicecandidate = myCandidateHandler;\n\nThe `eventType` parameter also accepts the special '*' value which forwards all known `RTCPeerConnection` events to a single handler.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flostsource%2Fsimplertcdata","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flostsource%2Fsimplertcdata","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flostsource%2Fsimplertcdata/lists"}