WebSockets

supports bidirectional communication between a client and server

For the server side, a controller action must be created:

1
2
3
4
5
6
7
func MySocket(w http.ResponseWriter, r *http.Request) {
controller.Act("my.socket", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
channel := "channel-a"
err = as.Services.Socket.Upgrade(rc, channel, ps.Profile, ps.Logger)
return "", err
})
}

On the client side, add this to your TypeScript:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function open() {
console.log("socket opened");
}

// Message: { channel: string, cmd: string, param: any }
function recv(m: Message) {
console.log("message received", m);
}

function err(svc: string, err: string) {
console.log("error received", svc, err);
}

document.addEventListener("DOMContentLoaded", function() {
// first argument is `debug`, which, when set to true, adds logs
new {your project}.Socket(true, open, recv, err, "/socket");
});