golang socket.io
golang socket.io is an implementation for the socket.io protocol in Go. There is a lack of specification for the socket.io protocol, so reverse engineering is the easiest way to find out how it works.
This is a work in progress. Many features, such as middleware and binary support, are missing.
golang socket.io is an adapted work from github.com/graarh/golang-socketio.
on "connection", "error", and "disconnection"
socket.io has three special events it triggers on the client-side and you should not emit them on your own programs.
Wait for the socket.io connection event before emitting messages or you risk losing them due in an unpredictable fashion (due to concurrency: connection latency, server load, etc.). For the default namespace this is automatically handled on gosocketio.Connect.
However, before emitting a message on a custom namespace, you want to wait for the ready signal, like so:
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()
select {
case <-ctx.Done():
return ctx.Err()
case <-exampleNamespace.Ready():
// don't need to do anything
}
if err := exampleNamespace.Emit("fleet", 100); err != nil {
return err
}
The reason why you probably want to use a select
receiving a second channel, such as context.Done() on all non-trivial programs is to avoid program loop, leak memory, or both in case of failure.
The default namespace is automatically ready after establishing the socket.io session. Therefore, *gosocketio.Client
doesn't expose a Ready()
method.
Connecting to a socket.io server with a custom namespace
You can connect to a namespace and start emitting messages to it with:
c, err := gosocketio.Connect(u, websocket.NewTransport())
if err != nil {
return err
}
// ...
exampleNamespace, err := c.Of("example")
If err != nil {
return err
}
<-exampleNamespace.Ready() // don't do this, use a select like shown above instead!
If err := exampleNamespace.Emit("list", "friends"); err != nil {
return err
}
Running the example
-
npm install
to install the dependencies for the example server node server.js
go run example.go
If you need to improve this library you should consider using these tools:
This library is used by the WeDeploy Command-Line Interface tool to open a shell and execute commands on Docker container services.