Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WebpeerJS</title>
  </head>
  <body>
    <h3>Basic Connection Demo of <a href="https://github.com/nuzulul/webpeerjs">WebpeerJS</a></h3>
    <div id="app"><ul id="myList"></ul></div>
    <script type="module">
    
        import {webpeerjs} from 'https://esm.sh/webpeerjs';
        
        void async function main() {
            function write(input){
                const node = document.createElement("li")
                const textnode = document.createTextNode(input)
                node.appendChild(textnode)
                document.getElementById("myList").appendChild(node)
            }
            const node = await webpeerjs.createWebpeer()
            
            write(`My node id : ${node.id}`)
            
            const room = 'globalroom'
            
            write(`joinRoom : ${room}`)
            
            const [broadcast,listen,members] = node.joinRoom(room)
            
            listen((message,id) => {
                write(`Message from ${id} : ${message}`)
            })
            
            members((data) => {
                write(`Members update : ${data}`)
                broadcast('hello')
            })
            
        }()
    </script>
  </body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers