Home

Awesome

Golang PeerJS

A Golang port of PeerJS

Implementation

Docs

Go Reference

⚠️ Note: While the Javascript PeerJS documentation often applies to this library, there are differences, namely:

Unsupported features

Usage example

See _examples folder


package main

import (
	"log"
	"time"

	peer "github.com/muka/peerjs-go"
)

func main() {
	peer1, _ := peer.NewPeer("peer1", peer.NewOptions())
	defer peer1.Close()

	peer2, _ := peer.NewPeer("peer2", peer.NewOptions())
	defer peer2.Close()

	peer2.On("connection", func(data interface{}) {
		conn2 := data.(*peer.DataConnection)
		conn2.On("data", func(data interface{}) {
			// Will print 'hi!'
			log.Printf("Received: %#v: %s\n", data, data)
		})
	})

	conn1, _ := peer1.Connect("peer2", nil)
	conn1.On("open", func(data interface{}) {
		for {
			conn1.Send([]byte("hi!"), false)
			<-time.After(time.Millisecond * 1000)
		}
	})

	select {}
}

Peer server

This library includes a GO based peer server in the /server folder

Documentation:

Go Reference

Example usage

package main

import (
	"log"

	peerjsServer "github.com/muka/peerjs-go/server"
)

func main() {
	serverOptions := peerjsServer.NewOptions()
	// These are the default values NewOptions() creates:
	serverOptions.Port = 9000
	serverOptions.Host = "0.0.0.0"
	serverOptions.LogLevel = "info"
	serverOptions.ExpireTimeout = 5000
	serverOptions.AliveTimeout = 60000
	serverOptions.Key = "peerjs"
	serverOptions.Path = "/"
	serverOptions.ConcurrentLimit = 5000
	serverOptions.AllowDiscovery = false
	serverOptions.CleanupOutMsgs = 1000

	server := peerjsServer.New(serverOptions)
	defer server.Stop()

	if err := server.Start(); err != nil {
		log.Printf("Error starting peerjs server: %s", err)
	}

	select{}
}

Docker

A docker image for the GO based peer server is available at opny/peer-server built for Raspberry Pi and PCs

Standalone

To build a standalone GO based Peerjs server executable, run go build ./cmd/server/main.go in the repository folder. To set the server options, create a peer.yaml configuration file in the same folder as the executable with the following options:

Available Server Options: