websocket
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
websocket
websocket is a minimal and idiomatic WebSocket library for Go.
Install
go get github.com/coder/websocket
[!NOTE]Coder now maintains this project as explained inthis blog post.We're grateful tonhooyr for authoring and maintaining this project from2019 to 2024.
Highlights
- Minimal and idiomatic API
- First classcontext.Context support
- Fully passes the WebSocketautobahn-testsuite
- Zero dependencies
- JSON helpers in thewsjson subpackage
- Zero alloc reads and writes
- Concurrent writes
- Close handshake
- net.Conn wrapper
- Ping pong API
- RFC 7692 permessage-deflate compression
- CloseRead helper for write only connections
- Compile toWasm
Roadmap
See GitHub issues for minor issues but the major future enhancements are:
- Perfect examples#217
- wstest.Pipe for in memory testing#340
- Ping pong heartbeat helper#267
- Ping pong instrumentation callbacks#246
- Graceful shutdown helpers#209
- Assembly for WebSocket masking#16
- WIP at#326, about 3x faster
- HTTP/2#4
- The holy grail#402
Examples
For a production quality example that demonstrates the complete API, see theecho example.
For a full stack example, see thechat example.
Server
http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {c, err := websocket.Accept(w, r, nil)if err != nil {// ...}defer c.CloseNow()// Set the context as needed. Use of r.Context() is not recommended// to avoid surprising behavior (see http.Hijacker).ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)defer cancel()var v interface{}err = wsjson.Read(ctx, c, &v)if err != nil {// ...}log.Printf("received: %v", v)c.Close(websocket.StatusNormalClosure, "")})
Client
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)defer cancel()c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)if err != nil {// ...}defer c.CloseNow()err = wsjson.Write(ctx, c, "hi")if err != nil {// ...}c.Close(websocket.StatusNormalClosure, "")
Comparison
gorilla/websocket
Advantages ofgorilla/websocket:
- Mature and widely used
- Prepared writes
- Configurablebuffer sizes
- No extra goroutine per connection to support cancellation with context.Context. This costs github.com/coder/websocket 2 KB of memory per connection.
- Will be removed soon withcontext.AfterFunc. See#411
Advantages of github.com/coder/websocket:
- Minimal and idiomatic API
- Compare godoc ofgithub.com/coder/websocket withgorilla/websocket side by side.
- net.Conn wrapper
- Zero alloc reads and writes (gorilla/websocket#535)
- Fullcontext.Context support
- Dial usesnet/http.Client
- Will enable easy HTTP/2 support in the future
- Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client.
- Concurrent writes
- Close handshake (gorilla/websocket#448)
- Idiomaticping pong API
- Gorilla requires registering a pong callback before sending a Ping
- Can target Wasm (gorilla/websocket#432)
- Transparent message buffer reuse withwsjson subpackage
- 1.75x faster WebSocket masking implementation in pure Go
- Fullpermessage-deflate compression extension support
- Gorilla only supports no context takeover mode
- CloseRead helper for write only connections (gorilla/websocket#492)
golang.org/x/net/websocket
golang.org/x/net/websocket is deprecated.Seegolang/go/issues/18152.
Thenet.Conn can help in transitioningto github.com/coder/websocket.
gobwas/ws
gobwas/ws has an extremely flexible API that allows it to be usedin an event driven style for performance. See the author'sblog post.
However it is quite bloated. Seehttps://pkg.go.dev/github.com/gobwas/ws
When writing idiomatic Go, github.com/coder/websocket will be faster and easier to use.
lesismal/nbio
lesismal/nbio is similar to gobwas/ws in that the API isevent driven for performance reasons.
However it is quite bloated. Seehttps://pkg.go.dev/github.com/lesismal/nbio
When writing idiomatic Go, github.com/coder/websocket will be faster and easier to use.
Documentation¶
Overview¶
Package websocket implements theRFC 6455 WebSocket protocol.
https://tools.ietf.org/html/rfc6455
Use Dial to dial a WebSocket server.
Use Accept to accept a WebSocket client.
Conn represents the resulting WebSocket connection.
The examples are the best way to understand how to correctly use the library.
The wsjson subpackage contain helpers for JSON and protobuf messages.
More documentation athttps://github.com/coder/websocket.
Wasm¶
The client side supports compiling to Wasm.It wraps the WebSocket browser API.
Seehttps://developer.mozilla.org/en-US/docs/Web/API/WebSocket
Some important caveats to be aware of:
- Accept always errors out
- Conn.Ping is no-op
- Conn.CloseNow is Close(StatusGoingAway, "")
- HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op
- *http.Response from Dial is &http.Response{} with a 101 status code on success
Example (CrossOrigin)¶
package mainimport ("log""net/http""github.com/coder/websocket")func main() {// This handler demonstrates how to safely accept cross origin WebSockets// from the origin example.com.fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {c, err := websocket.Accept(w, r, &websocket.AcceptOptions{OriginPatterns: []string{"example.com"},})if err != nil {log.Println(err)return}c.Close(websocket.StatusNormalClosure, "cross origin WebSocket accepted")})err := http.ListenAndServe("localhost:8080", fn)log.Fatal(err)}
Example (Echo)¶
This example demonstrates a echo server.
package mainimport ()func main() {// https://github.com/nhooyr/websocket/tree/master/internal/examples/echo}
Example (FullStackChat)¶
This example demonstrates full stack chat with an automated test.
package mainimport ()func main() {// https://github.com/nhooyr/websocket/tree/master/internal/examples/chat}
Example (WriteOnly)¶
package mainimport ("context""log""net/http""time""github.com/coder/websocket""github.com/coder/websocket/wsjson")func main() {// This handler demonstrates how to correctly handle a write only WebSocket connection.// i.e you only expect to write messages and do not expect to read any messages.fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {c, err := websocket.Accept(w, r, nil)if err != nil {log.Println(err)return}defer c.CloseNow()ctx, cancel := context.WithTimeout(r.Context(), time.Minute*10)defer cancel()ctx = c.CloseRead(ctx)t := time.NewTicker(time.Second * 30)defer t.Stop()for {select {case <-ctx.Done():c.Close(websocket.StatusNormalClosure, "")returncase <-t.C:err = wsjson.Write(ctx, c, "hi")if err != nil {log.Println(err)return}}}})err := http.ListenAndServe("localhost:8080", fn)log.Fatal(err)}
Index¶
- func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn
- type AcceptOptions
- type CloseError
- type CompressionMode
- type Conn
- func (c *Conn) Close(code StatusCode, reason string) (err error)
- func (c *Conn) CloseNow() (err error)
- func (c *Conn) CloseRead(ctx context.Context) context.Context
- func (c *Conn) Ping(ctx context.Context) error
- func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error)
- func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error)
- func (c *Conn) SetReadLimit(n int64)
- func (c *Conn) Subprotocol() string
- func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error
- func (c *Conn) Writer(ctx context.Context, typ MessageType) (io.WriteCloser, error)
- type DialOptions
- type MessageType
- type StatusCode
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcNetConn¶
NetConn converts a *websocket.Conn into a net.Conn.
It's for tunneling arbitrary protocols over WebSockets.Few users of the library will need this but it's tricky to implementcorrectly and so provided in the library.Seehttps://github.com/nhooyr/websocket/issues/100.
Every Write to the net.Conn will correspond to a message write ofthe given type on *websocket.Conn.
The passed ctx bounds the lifetime of the net.Conn. If cancelled,all reads and writes on the net.Conn will be cancelled.
If a message is read that is not of the correct type, the connectionwill be closed with StatusUnsupportedData and an error will be returned.
Close will close the *websocket.Conn with StatusNormalClosure.
When a deadline is hit and there is an active read or write goroutine, theconnection will be closed. This is different from most net.Conn implementationswhere only the reading/writing goroutines are interrupted but the connectionis kept alive.
The Addr methods will return the real addresses for connections obtainedfrom websocket.Accept. But for connections obtained from websocket.Dial, a mock net.Addrwill be returned that gives "websocket" for Network() and "websocket/unknown-addr" forString(). This is because websocket.Dial only exposes a io.ReadWriteCloser instead of thefull net.Conn to us.
When running as WASM, the Addr methods will always return the mock address described above.
A received StatusNormalClosure or StatusGoingAway close frame will be translated toio.EOF when reading.
Furthermore, the ReadLimit is set to -1 to disable it.
Types¶
typeAcceptOptions¶
type AcceptOptions struct {// Subprotocols lists the WebSocket subprotocols that Accept will negotiate with the client.// The empty subprotocol will always be negotiated as perRFC 6455. If you would like to// reject it, close the connection when c.Subprotocol() == "".Subprotocols []string// InsecureSkipVerify is used to disable Accept's origin verification behaviour.//// You probably want to use OriginPatterns instead.InsecureSkipVerifybool// OriginPatterns lists the host patterns for authorized origins.// The request host is always authorized.// Use this to enable cross origin WebSockets.//// i.e javascript running on example.com wants to access a WebSocket server at chat.example.com.// In such a case, example.com is the origin and chat.example.com is the request host.// One would set this field to []string{"example.com"} to authorize example.com to connect.//// Each pattern is matched case insensitively against the request origin host// with path.Match.// Seehttps://golang.org/pkg/path/#Match//// Please ensure you understand the ramifications of enabling this.// If used incorrectly your WebSocket server will be open to CSRF attacks.//// Do not use * as a pattern to allow any origin, prefer to use InsecureSkipVerify instead// to bring attention to the danger of such a setting.OriginPatterns []string// CompressionMode controls the compression mode.// Defaults to CompressionDisabled.//// See docs on CompressionMode for details.CompressionModeCompressionMode// CompressionThreshold controls the minimum size of a message before compression is applied.//// Defaults to 512 bytes for CompressionNoContextTakeover and 128 bytes// for CompressionContextTakeover.CompressionThresholdint// OnPingReceived is an optional callback invoked synchronously when a ping frame is received.//// The payload contains the application data of the ping frame.// If the callback returns false, the subsequent pong frame will not be sent.// To avoid blocking, any expensive processing should be performed asynchronously using a goroutine.OnPingReceived func(ctxcontext.Context, payload []byte)bool// OnPongReceived is an optional callback invoked synchronously when a pong frame is received.//// The payload contains the application data of the pong frame.// To avoid blocking, any expensive processing should be performed asynchronously using a goroutine.//// Unlike OnPingReceived, this callback does not return a value because a pong frame// is a response to a ping and does not trigger any further frame transmission.OnPongReceived func(ctxcontext.Context, payload []byte)}
AcceptOptions represents Accept's options.
typeCloseError¶
type CloseError struct {CodeStatusCodeReasonstring}
CloseError is returned when the connection is closed with a status and reason.
Use Go 1.13's errors.As to check for this error.Also see the CloseStatus helper.
func (CloseError)Error¶
func (ceCloseError) Error()string
typeCompressionMode¶
type CompressionModeint
CompressionMode represents the modes available to the permessage-deflate extension.Seehttps://tools.ietf.org/html/rfc7692
Works in all modern browsers except Safari which does not implement the permessage-deflate extension.
Compression is only used if the peer supports the mode selected.
const (// CompressionDisabled disables the negotiation of the permessage-deflate extension.//// This is the default. Do not enable compression without benchmarking for your particular use case first.CompressionDisabledCompressionMode =iota// CompressionContextTakeover compresses each message greater than 128 bytes reusing the 32 KB sliding window from// previous messages. i.e compression context across messages is preserved.//// As most WebSocket protocols are text based and repetitive, this compression mode can be very efficient.//// The memory overhead is a fixed 32 KB sliding window, a fixed 1.2 MB flate.Writer and a sync.Pool of 40 KB flate.Reader's// that are used when reading and then returned.//// Thus, it uses more memory than CompressionNoContextTakeover but compresses more efficiently.//// If the peer does not support CompressionContextTakeover then we will fall back to CompressionNoContextTakeover.CompressionContextTakeover// CompressionNoContextTakeover compresses each message greater than 512 bytes. Each message is compressed with// a new 1.2 MB flate.Writer pulled from a sync.Pool. Each message is read with a 40 KB flate.Reader pulled from// a sync.Pool.//// This means less efficient compression as the sliding window from previous messages will not be used but the// memory overhead will be lower as there will be no fixed cost for the flate.Writer nor the 32 KB sliding window.// Especially if the connections are long lived and seldom written to.//// Thus, it uses less memory than CompressionContextTakeover but compresses less efficiently.//// If the peer does not support CompressionNoContextTakeover then we will fall back to CompressionDisabled.CompressionNoContextTakeover)
typeConn¶
type Conn struct {// contains filtered or unexported fields}
Conn represents a WebSocket connection.All methods may be called concurrently except for Reader and Read.
You must always read from the connection. Otherwise controlframes will not be handled. See Reader and CloseRead.
Be sure to call Close on the connection when youare finished with it to release associated resources.
On any error from any method, the connection is closedwith an appropriate reason.
This applies to context expirations as well unfortunately.Seehttps://github.com/nhooyr/websocket/issues/242#issuecomment-633182220
funcAccept¶
func Accept(whttp.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn,error)
Accept accepts a WebSocket handshake from a client and upgrades thethe connection to a WebSocket.
Accept will not allow cross origin requests by default.See the InsecureSkipVerify and OriginPatterns options to allow cross origin requests.
Accept will write a response to w on all errors.
Note that using the http.Request Context after Accept returns may lead tounexpected behavior (see http.Hijacker).
Example¶
package mainimport ("context""log""net/http""time""github.com/coder/websocket""github.com/coder/websocket/wsjson")func main() {// This handler accepts a WebSocket connection, reads a single JSON// message from the client and then closes the connection.fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {c, err := websocket.Accept(w, r, nil)if err != nil {log.Println(err)return}defer c.CloseNow()ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)defer cancel()var v interface{}err = wsjson.Read(ctx, c, &v)if err != nil {log.Println(err)return}c.Close(websocket.StatusNormalClosure, "")})err := http.ListenAndServe("localhost:8080", fn)log.Fatal(err)}
funcDial¶
Dial performs a WebSocket handshake on url.
The response is the WebSocket handshake response from the server.You never need to close resp.Body yourself.
If an error occurs, the returned response may be non nil.However, you can only read the first 1024 bytes of the body.
This function requires at least Go 1.12 as it uses a new featurein net/http to perform WebSocket handshakes.See docs on the HTTPClient option andhttps://github.com/golang/go/issues/26937#issuecomment-415855861
URLs with http/https schemes will work and are interpreted as ws/wss.
Example¶
package mainimport ("context""log""time""github.com/coder/websocket""github.com/coder/websocket/wsjson")func main() {// Dials a server, writes a single JSON message and then// closes the connection.ctx, cancel := context.WithTimeout(context.Background(), time.Minute)defer cancel()c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)if err != nil {log.Fatal(err)}defer c.CloseNow()err = wsjson.Write(ctx, c, "hi")if err != nil {log.Fatal(err)}c.Close(websocket.StatusNormalClosure, "")}
func (*Conn)Close¶
func (c *Conn) Close(codeStatusCode, reasonstring) (errerror)
Close performs the WebSocket close handshake with the given status code and reason.
It will write a WebSocket close frame with a timeout of 5s and then wait 5s forthe peer to send a close frame.All data messages received from the peer during the close handshake will be discarded.
The connection can only be closed once. Additional calls to Closeare no-ops.
The maximum length of reason must be 125 bytes. Avoid sending a dynamic reason.
Close will unblock all goroutines interacting with the connection oncecomplete.
func (*Conn)CloseNow¶
CloseNow closes the WebSocket connection without attempting a close handshake.Use when you do not want the overhead of the close handshake.
func (*Conn)CloseRead¶
CloseRead starts a goroutine to read from the connection until it is closedor a data message is received.
Once CloseRead is called you cannot read any messages from the connection.The returned context will be cancelled when the connection is closed.
If a data message is received, the connection will be closed with StatusPolicyViolation.
Call CloseRead when you do not expect to read any more messages.Since it actively reads from the connection, it will ensure that ping, pong and closeframes are responded to. This means c.Ping and c.Close will still work as expected.
This function is idempotent.
func (*Conn)Ping¶
Ping sends a ping to the peer and waits for a pong.Use this to measure latency or ensure the peer is responsive.Ping must be called concurrently with Reader as it doesnot read from the connection but instead waits for a Reader callto read the pong.
TCP Keepalives should suffice for most use cases.
Example¶
package mainimport ("context""log""time""github.com/coder/websocket")func main() {// Dials a server and pings it 5 times.ctx, cancel := context.WithTimeout(context.Background(), time.Minute)defer cancel()c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)if err != nil {log.Fatal(err)}defer c.CloseNow()// Required to read the Pongs from the server.ctx = c.CloseRead(ctx)for i := 0; i < 5; i++ {err = c.Ping(ctx)if err != nil {log.Fatal(err)}}c.Close(websocket.StatusNormalClosure, "")}
func (*Conn)Read¶
Read is a convenience method around Reader to read a single messagefrom the connection.
func (*Conn)Reader¶
Reader reads from the connection until there is a WebSocketdata message to be read. It will handle ping, pong and close frames as appropriate.
It returns the type of the message and an io.Reader to read it.The passed context will also bound the reader.Ensure you read to EOF otherwise the connection will hang.
Call CloseRead if you do not expect any data messages from the peer.
Only one Reader may be open at a time.
If you need a separate timeout on the Reader call and the Read itself,use time.AfterFunc to cancel the context passed in.Seehttps://github.com/nhooyr/websocket/issues/87#issue-451703332Most users should not need this.
func (*Conn)SetReadLimit¶
SetReadLimit sets the max number of bytes to read for a single message.It applies to the Reader and Read methods.
By default, the connection has a message read limit of 32768 bytes.
When the limit is hit, the connection will be closed with StatusMessageTooBig.
Set to -1 to disable.
func (*Conn)Subprotocol¶
Subprotocol returns the negotiated subprotocol.An empty string means the default protocol.
func (*Conn)Write¶
Write writes a message to the connection.
See the Writer method if you want to stream a message.
If compression is disabled or the compression threshold is not met, then itwill write the message in a single frame.
func (*Conn)Writer¶
func (c *Conn) Writer(ctxcontext.Context, typMessageType) (io.WriteCloser,error)
Writer returns a writer bounded by the context that will writea WebSocket message of type dataType to the connection.
You must close the writer once you have written the entire message.
Only one writer can be open at a time, multiple calls will block until the previous writeris closed.
typeDialOptions¶
type DialOptions struct {// HTTPClient is used for the connection.// Its Transport must return writable bodies for WebSocket handshakes.// http.Transport does beginning with Go 1.12.HTTPClient *http.Client// HTTPHeader specifies the HTTP headers included in the handshake request.HTTPHeaderhttp.Header// Host optionally overrides the Host HTTP header to send. If empty, the value// of URL.Host will be used.Hoststring// Subprotocols lists the WebSocket subprotocols to negotiate with the server.Subprotocols []string// CompressionMode controls the compression mode.// Defaults to CompressionDisabled.//// See docs on CompressionMode for details.CompressionModeCompressionMode// CompressionThreshold controls the minimum size of a message before compression is applied.//// Defaults to 512 bytes for CompressionNoContextTakeover and 128 bytes// for CompressionContextTakeover.CompressionThresholdint// OnPingReceived is an optional callback invoked synchronously when a ping frame is received.//// The payload contains the application data of the ping frame.// If the callback returns false, the subsequent pong frame will not be sent.// To avoid blocking, any expensive processing should be performed asynchronously using a goroutine.OnPingReceived func(ctxcontext.Context, payload []byte)bool// OnPongReceived is an optional callback invoked synchronously when a pong frame is received.//// The payload contains the application data of the pong frame.// To avoid blocking, any expensive processing should be performed asynchronously using a goroutine.//// Unlike OnPingReceived, this callback does not return a value because a pong frame// is a response to a ping and does not trigger any further frame transmission.OnPongReceived func(ctxcontext.Context, payload []byte)}
DialOptions represents Dial's options.
typeMessageType¶
type MessageTypeint
MessageType represents the type of a WebSocket message.Seehttps://tools.ietf.org/html/rfc6455#section-5.6
const (// MessageText is for UTF-8 encoded text messages like JSON.MessageTextMessageType =iota + 1// MessageBinary is for binary messages like protobufs.MessageBinary)
MessageType constants.
func (MessageType)String¶
func (iMessageType) String()string
typeStatusCode¶
type StatusCodeint
StatusCode represents a WebSocket status code.https://tools.ietf.org/html/rfc6455#section-7.4
const (StatusNormalClosureStatusCode = 1000StatusGoingAwayStatusCode = 1001StatusProtocolErrorStatusCode = 1002StatusUnsupportedDataStatusCode = 1003// StatusNoStatusRcvd cannot be sent in a close message.// It is reserved for when a close message is received without// a status code.StatusNoStatusRcvdStatusCode = 1005// StatusAbnormalClosure is exported for use only with Wasm.// In non Wasm Go, the returned error will indicate whether the// connection was closed abnormally.StatusAbnormalClosureStatusCode = 1006StatusInvalidFramePayloadDataStatusCode = 1007StatusPolicyViolationStatusCode = 1008StatusMessageTooBigStatusCode = 1009StatusMandatoryExtensionStatusCode = 1010StatusInternalErrorStatusCode = 1011StatusServiceRestartStatusCode = 1012StatusTryAgainLaterStatusCode = 1013StatusBadGatewayStatusCode = 1014// StatusTLSHandshake is only exported for use with Wasm.// In non Wasm Go, the returned error will indicate whether there was// a TLS handshake failure.StatusTLSHandshakeStatusCode = 1015)
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
You can define custom codes in the 3000-4999 range.The 3000-3999 range is reserved for use by libraries, frameworks and applications.The 4000-4999 range is reserved for private use.
funcCloseStatus¶
func CloseStatus(errerror)StatusCode
CloseStatus is a convenience wrapper around Go 1.13's errors.As to grabthe status code from a CloseError.
-1 will be returned if the passed error is nil or not a CloseError.
Example¶
package mainimport ("context""log""time""github.com/coder/websocket")func main() {// Dials a server and then expects to be disconnected with status code// websocket.StatusNormalClosure.ctx, cancel := context.WithTimeout(context.Background(), time.Minute)defer cancel()c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)if err != nil {log.Fatal(err)}defer c.CloseNow()_, _, err = c.Reader(ctx)if websocket.CloseStatus(err) != websocket.StatusNormalClosure {log.Fatalf("expected to be disconnected with StatusNormalClosure but got: %v", err)}}
func (StatusCode)String¶
func (iStatusCode) String()string
Source Files¶
Directories¶
Path | Synopsis |
---|---|
internal | |
test Package test contains subpackages only used in tests. | Package test contains subpackages only used in tests. |
wsjs Package wsjs implements typed access to the browser javascript WebSocket API. | Package wsjs implements typed access to the browser javascript WebSocket API. |
thirdpartyModule | |
Package wsjson provides helpers for reading and writing JSON messages. | Package wsjson provides helpers for reading and writing JSON messages. |