Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Last active November 6, 2022 11:00
Show Gist options
  • Save jeromeetienne/4c56e173f17b11c40872fa31cf71e96b to your computer and use it in GitHub Desktop.
Save jeromeetienne/4c56e173f17b11c40872fa31cf71e96b to your computer and use it in GitHub Desktop.
peerjs example - standalone - one webpage sending data to another
<script src="http://cdn.peerjs.com/0.3/peer.min.js"></script>
<script type="text/javascript">
// https://github.com/peers/peerjs
// go here to get your own key
var peerjsPeer = new Peer({key: 'xxxYOURKEYGOESHERExxxx'});
peerjsPeer.on('open', function() {
console.log('My peerjsPeer ID is: ', peerjsPeer.id);
});
peerjsPeer.on('connection', function(peerjsConnection) {
peerjsConnection.on('open', function() {
// Receive messages
peerjsConnection.on('data', function(data) {
console.log('Received', data);
});
// Send messages
peerjsConnection.send('Hello from markers-page!');
});
});
// var conn = peer.connect('arjs-marker-page');
</script>
<script src="http://cdn.peerjs.com/0.3/peer.min.js"></script>
<script type="text/javascript">
var peer = new Peer({key: 'xxxYOURKEYGOESHERExxxx'});
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
});
function connect(peerID){
var conn = peer.connect(peerID);
conn.on('open', function() {
// Receive messages
conn.on('data', function(data) {
console.log('Received', data);
});
// Send messages
conn.send('Hello from phone!');
});
}
</script>
@ToxicalNoob3062
Copy link

excellent !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment