Golang implementation of a STOMP client over WebSocket.
- Establishing a STOMP connection
- Subscribing to events
To start using the STOMP client:
- Define the connection URL
- Create the STOMP client using either a token or a custom Dial
- Define a channel to receive frames
To connect to the Watch API of Tenant-Manager, which uses the STOMP protocol and has the following URL:
ws://tenant-manager:8080/api/v3/tenant-manager/watch
token, _ := tenantWatchClient.Credential.GetAuthToken()
url, _ := url.Parse("ws://localhost:8080/api/v3/tenant-manager/watch")
stompClient, _ := go_stomp_websocket.ConnectWithToken(*url, token)type ConnectionDialer interface {
Dial(webSocketURL url.URL, dialer websocket.Dialer, requestHeaders http.Header) (*websocket.Conn, *http.Response, error)
}url, _ := url.Parse("ws://localhost:8080/api/v3/tenant-manager/watch")
dialer := websocket.Dialer{}
// configure the dialer
requestHeaders := http.Header{}
// add headers
connDial := ConnectionDialerImpl{} // implements the Dial method of ConnectionDialer interface
stompClient, _ := go_stomp_websocket.Connect(*url, dialer, requestHeaders, connDial)Subscribe to events:
subscr, _ := stompClient.Subscribe("/tenant-changed")Handle received frames:
go func() {
for {
var tenant = new(tenant.Tenant)
frame := <-subscr.FrameCh // Receive frame
if len(frame.Body) > 0 {
err := json.Unmarshal([]byte(frame.Body), tenant) // Parse the frame body into Tenant structure
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Received tenant with id:%s\n", tenant.ObjectId)
}
}
}
}()