modulepackage
0.0.0-20240401061429-5b28944b9e4f
Repository: https://github.com/muka/peerjs-go.git
Documentation: pkg.go.dev
# README
Golang PeerJS
A Golang port of PeerJS
Implementation
- Datachannel
- Mediachannel
- Test coverage > 80%
- Signalling server
- Interoperability tests
Docs
⚠️ Note: While the Javascript PeerJS documentation often applies to this library, there are differences, namely:
- All methods and properties are in PascalCase.
- Enum values are represented as seperate constants.
- All peer event callback functions should take a generic interface{} parameter, and then cast the interface{} to the appropriate peerjs-go type.
- Blocked peer event callback functions will block other peerjs-go events from firing.
- Refer to the go package docs whenever unsure.
Unsupported features
- Payload de/encoding based on js-binarypack is not supported.
- Message chunking (should be already done in recent browser versions)
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:
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:
- Host String
- Port Int
- LogLevel String
- ExpireTimeout Int64
- AliveTimeout Int64
- Key String
- Path String
- ConcurrentLimit Int
- AllowDiscovery Bool
- CleanupOutMsgs Int
# Functions
NewAPI initiate a new API client.
NewConnectionOptions return a ConnectionOptions with defaults.
NewDataConnection create new DataConnection.
NewMediaConnection create new MediaConnection.
NewMediaStreamWithTrack create a mediastream with tracks.
NewNegotiator initiate a new negotiator.
NewOptions return Peer options with defaults.
NewPeer initializes a new Peer object.
NewSocket create a socket instance.
# Constants
DataChannelIDPrefix used as prefix for random ID.
DefaultBrowser is the browser name.
MaxBufferedAmount max amount to buffer.
MediaChannelIDPrefix the media channel connection id prefix.
# Variables
DefaultKey is the default API key.
# Structs
AnswerOption wraps answer options.
API wrap calls to API server.
BaseConnection shared base connection.
ConnectionOptions wrap optios for Peer Connect().
DataConnection track a connection with a remote Peer.
MediaConnection track a connection with a remote Peer.
MediaStream A stream of media content.
Negotiator manages all negotiations between Peers.
Options store Peer options.
Peer expose the PeerJS API.
No description provided by the author
Socket abstract websocket exposing an event emitter like interface.
SocketEvent carries an event from the socket.
# Interfaces
Connection shared interface.
MediaStreamTrack interaface that wraps together TrackLocal and TrackRemote.