go-socket.io
Socket.IO server and client with namespaces, rooms and acknowledgements, for socket.io-client 1.x and 2.x
About go-socket.io
go-socket.io
A Socket.IO server and Go client with namespaces, rooms, acknowledgements,
broadcast and a Redis adapter for multi-instance deployments. Engine.IO is included as
the engineio sub-package and can be used on its own.
This module is a maintained successor of the archived googollee/go-socket.io for
socket.io-client 1.x and 2.x. Applications moving from upstream v1.4.2 or v1.7.0 should
follow the migration guide.
Compatibility
| Server | Socket.IO protocol | Engine.IO protocol | JavaScript client |
|---|---|---|---|
| v1 (this module, repository root) | v4 | v3 | socket.io-client 1.x and 2.x |
v2 (in development in v2/, no runtime yet) | v5 | v4 | socket.io-client 3.x and 4.x |
The client versions and the reference server that the v1 interoperability checks use are listed in docs/PARITY.md. V1 protocol details and deviations: docs/PROTOCOL.md. The parts of the socket.io 2.x JavaScript API without a counterpart, or with a different one, are listed in docs/V1.md.
Install
Install the module:
go get github.com/sshaplygin/[email protected]
Go 1.22 or later is required.
Quick start
A server whose join handler adds the socket to the lobby room, broadcasts the client's
name to that room as joined and returns the ACK:
package main
import (
"log"
"net/http"
socketio "github.com/sshaplygin/go-socket.io"
)
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
server := socketio.NewServer()
defer func() { _ = server.Close() }()
root := server.Of("/")
root.OnEvent("join", func(s socketio.Conn, name string) string {
s.Join("lobby")
if err := root.To("lobby").Emit("joined", name); err != nil {
log.Print(err)
}
return "welcome, " + name // sent as the ACK when the client asks for one
})
go func() {
if err := server.Serve(); err != nil {
log.Print(err)
}
}()
http.Handle("/socket.io/", server)
return http.ListenAndServe(":8000", nil)
}
A JavaScript client connected to http://localhost:8000 that calls
socket.emit("join", "Ann", ack => console.log(ack)) receives joined with Ann and the
ACK welcome, Ann.
Examples
Runnable applications and launch instructions are in _examples/. Start with the net/http server or the Go client. The directory also includes ACKs, binary events, namespaces, middleware, framework integrations, Redis and graceful shutdown.
To try Socket.IO in the browser, open the online lab.
Usage
The Go client is available in
github.com/sshaplygin/go-socket.io/client;
server and client use separate connection types and constructors.
Configuration, namespace lifecycle, rooms, deployment behind HTTP servers and load balancers, adapters and typed events: V1 usage. API reference: Socket.IO and Engine.IO.
Contributing
See CONTRIBUTING.md for the PR process and CLAUDE.md for how to build and test. Report vulnerabilities privately as described in SECURITY.md.
License
BSD 3-Clause, see LICENSE.
Frequently Asked Questions
What is go-socket.io?
go-socket.io is a Networking library for the Go programming language. Socket.IO server and client with namespaces, rooms and acknowledgements, for socket.io-client 1.x and 2.x
How do I install go-socket.io?
Install go-socket.io with the Go module system using `go get sshaplygin/go-socket.io`. Check the repository for the current installation instructions.
What category does go-socket.io belong to?
go-socket.io is listed under Networking, specifically Networking.