SignalR server and client in go
SignalR is an open-source library that simplifies adding real-time web functionality to apps. Real-time web functionality enables server-side code to push content to clients instantly.
Historically it was tied to ASP.NET Core but the protocol is open and implementable in any language.
This repository contains an implementation of a SignalR server and a SignalR client in go. The implementation is based on the work of David Fowler at https://github.com/davidfowl/signalr-ports. Client and server support transport over WebSockets, Server Sent Events and raw TCP. Protocol encoding in JSON and MessagePack is fully supported.
With a correctly configured Go toolchain:
go get -u github.com/philippseith/signalrSignalR uses a signalr.HubInterface instance to anchor the connection on the server and a javascript HubConnection object to anchor the connection on the client.
The easiest way to implement the signalr.HubInterface in your project is to declare your own type and embed signalr.Hub which implements that interface and will take care of all the signalr plumbing. You can call your custom type anything you want so long as it implements the signalr.HubInterface interface.
package main
import "github.com/philippseith/signalr"
type AppHub struct {
signalr.Hub
}Add functions with your custom hub type as a receiver.
func (h *AppHub) SendChatMessage(message string) {
h.Clients().All().Send("chatMessageReceived", message)
}These functions must be public so that they can be seen by the signalr server package but can be invoked client-side as lowercase message names. We'll explain setting up the client side in a moment, but as a preview, here's an example of calling our AppHub.SendChatMessage(...) method from the client:
// javascript snippet invoking that AppHub.Send method from the client
connection.invoke('sendChatMessage', val);The signalr.HubInterface contains a pair of methods you can implement to handle connection and disconnection events. signalr.Hub contains empty implementations of them to satisfy the interface, but you can "override" those defaults by implementing your own functions with your custom hub type as a receiver:
func (c *chat) OnConnected(connectionID string) {
fmt.Printf("%s connected\n", connectionID)
}
func (c *chat) OnDisconnected(connectionID string) {
fmt.Printf("%s disconnected\n", connectionID)
}…Microsoft has published the client-side libraries as a node package with embedded typescript annotations: @microsoft/signalr.
You can install @microsoft/signalr through any node package manager:
| package manager | command |
|---|---|
| npm | npm install @microsoft/signalr@latest |
| yarn | yarn add @microsoft/signalr@latest |
| LibMan | libman install @microsoft/signalr@latest -p unpkg -d wwwroot/js/signalr --files dist/browser/signalr.js --files dist/browser/signalr.min.js --files dist/browser/signalr.map.js |
| none | you can download the version we are using in our chatsample from here (the minified version is here) |
How you format your client UI is going to depend on your application use case but here is a simple example. It illustrates the basic steps of connecting to your server hub:
import the signalr.js library (or signalr.min.js);
create a connection object using the HubConnectionBuilder;
bind events
connection.invoke(targetMethod, payload) to send invoke functions on the server hub;start your connection
To handle callbacks from the server, create a receiver class which gets the server callbacks mapped to its methods:
type receiver struct {
signalr.Hub
}
func (c *receiver) Receive(msg string) {
fmt.Println(msg)
}Receive gets called when the server does something like this:
hub.Clients().Caller().Send("receive", message)The client itself might be used like that:
// Create a Connection (with timeout for the negotiation process)
creationCtx, _ := context.WithTimeout(ctx, 2 * time.Second)
conn, err := signalr.NewHTTPConnection(creationCtx, address)
if err != nil {
return err
}
// Create the client and set a receiver for callbacks from the server
client, err := signalr.NewClient(ctx,
signalr.WithConnection(conn),
signalr.WithReceiver(receiver))
if err != nil {
return err
}
// Start the client loop
c.Start()
// Do some client work
ch := <-c.Invoke("update", data)
// ch gets the result of the update operationServer, Client and the protocol implementations are able to log most of their operations. The logging option is disabled
by default in all tests. To configure logging, edit the testLogConf.json file:
{
"Enabled": false,
"Debug": false
}Enabled is set to true, the logging will be enabled. The tests will log to os.Stderr.Debug ist set to true, the logging will be more detailed.No open issues yet, or sync has not completed.