Skip to content
btwiusePublic

About

An implementation of a libp2p transport using WebTeleport over WebSocket

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

wsport — WebSocket transport for go-libp2p

go.dev reference Go 1.25+ License Ask DeepWiki

A fork of go-ws-transport with support for mounting the WebSocket upgrade handler on an existing http.ServeMux.

Usage

import "github.com/btwiuse/wsport"

As a libp2p transport

host, _ := libp2p.New(
    libp2p.Transport(wsport.New),
)

Share a port with HTTP

Mount the WebSocket handler on the same http.ServeMux as your API routes:

mux := http.NewServeMux()
mux.HandleFunc("/api", apiHandler)

host, _ := libp2p.New(
    libp2p.Transport(wsport.New, wsport.WithMux(mux, "/p2p")),
)

ln, _ := net.Listen("tcp", ":8080")
host.Network().Listen(ma.StringCast("/ip4/0.0.0.0/tcp/8080/x-parity-ws/%2Fp2p"))
http.Serve(ln, mux)

Or with WebSocketHandler for full control

mux.Handle("/p2p", tpt.WebSocketHandler())

Order matters: WebSocketHandler must be called before Listen. If Listen is called first, handlerLn is nil and it falls through to the normal net.Listener path instead.

See cmd/muxp2p for a complete example.

About

An implementation of a libp2p transport using WebTeleport over WebSocket

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

⌂ 换网址