Skip to content

mutexstream/peerjs-go

 
 

Repository files navigation

Golang PeerJS

A Golang port of PeerJS

Implementation

  • Datachannel
  • Mediachannel
  • Test coverage > 80%
  • Signalling server
  • Interoperability tests

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 {}
}

Further documentation can be found at: https://pkg.go.dev/github.com/muka/peerjs-go/

Peer server

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

The source of the GO based peer server can be found in the server folder which can be imported as the go package "github.com/muka/peerjs-go/server" and is documentated on pkg.go.dev. Also see example usage in the peer_test.go file.

If you want a standalone GO based Peerjs server, run go build ./cmd/server/main.go to get an exacutable. To set the server options, create a peer.yaml configuration file in the same folder as the executable. Available Server Options:

  • Host String
  • Port Int
  • LogLevel String
  • ExpireTimeout Int64
  • AliveTimeout Int64
  • Key String
  • Path String
  • ConcurrentLimit Int
  • AllowDiscovery Bool

Unsupported features

  • Payload de/encoding based on js-binarypack is not supported.
  • Message chunking (should be already done in recent browser versions)

About

Golang PeerJS implementation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 97.1%
  • Makefile 1.9%
  • Other 1.0%