Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 01:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
NodeJS Peer-To-Peer

NODEJS PEER TO PEER

https://code-boxx.com/simple-webrtc-javascript-nodejs/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Install required modules - npm i express peer
    • Start the Express and Peer server - node 1-server.js.
  2. Access http://localhost/a in your browser, open the developer's console.
  3. Open another tab or use another browser, access http://localhost/b - This will send a direct P2P message to http://localhost/a.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) REQUIRED MODULES
// npm install express peer
const express = require("express"),
path = require("path"),
{ PeerServer } = require("peer");
// (B) EXPRESS SERVER
// http://localhost/a
// http://localhost/b
const app = express();
app.get("/a", (req, res) => res.sendFile(path.join(__dirname, "2-peer-a.html")));
app.get("/b", (req, res) => res.sendFile(path.join(__dirname, "3-peer-b.html")));
app.listen(80);
// (C) PEER SERVER
// http://localhost:9000/myapp
const peerServer = PeerServer({
port: 9000,
path: "/myapp"
});
<!DOCTYPE html>
<html>
<head>
<title>WEBRTC (Peer To Peer)</title>
</head>
<body>
<!-- (A) LOAD PEERJS -->
<script src="https://unpkg.com/peerjs@1.4.7/dist/peerjs.min.js"></script>
<!-- (B) PEER-TO-PEER -->
<script>
// (B1) HANDSHAKE WITH PEER SERVER
const peer = new Peer("PEER-A", {
host: "localhost",
port: 9000,
path: "/myapp"
});
// (B2) READY
peer.on("open", id => console.log("Your ID is " + id));
// (B3) ON RECEIVING MESSAGE FROM OTHER PEERS
peer.on("connection", conn => {
conn.on("data", data => console.log(data));
// conn.close();
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>WEBRTC (Peer To Peer)</title>
</head>
<body>
<!-- (A) LOAD PEERJS -->
<script src="https://unpkg.com/peerjs@1.4.7/dist/peerjs.min.js"></script>
<!-- (B) PEER-TO-PEER -->
<script>
// (B1) HANDSHAKE WITH PEER SERVER
const peer = new Peer("PEERB", {
host: "localhost",
port: 9000,
path: "/myapp"
});
// (B2) READY - CONNECT & SEND MESSAGE TO PEER A
peer.on("open", id => {
console.log("Your ID is " + id);
var conn = peer.connect("PEER-A");
conn.on("open", () => conn.send("Hi from PEER-B!"));
});
</script>
</body>
</html>
call npm i express peer
node 1-server.js
source "npm i express peer"
node ./1-server.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment