Go-PTS is a flexible package for managing Pub-Sub over WebSockets in Go. It offers a rest-style syntax and easily integrates with various websocket and http frameworks.
- Get the
go-pts
package using the following command:
go get github.com/mono424/go-pts
To use go-pts with a specific websocket library, you need to install the corresponding connector.
go get github.com/mono424/go-pts-gorilla-connector
Then, you can import them in your code like this:
import (
"github.com/mono424/go-pts"
ptsc_gorilla "github.com/mono424/go-pts-gorilla-connector"
)
For client-side integration, you can use one of the following client libraries:
Language | URL |
---|---|
JavaScript | go-pts-client-js |
Dart | go-pts-client-dart |
For server-side integration with WebSocket libraries, you can use one of the following connectors:
WebSocket Library | URL |
---|---|
Gorilla WebSocket | go-pts-gorilla-connector |
Melody | go-pts-melody-connector |
- Create a new TubeSystem
tubeSystem := pts.New(ptsc_gorilla.NewConnector(
websocket.Upgrader{},
func(err *pts.Error) {
println(err.Description)
},
))
- Register Channels
tubeSystem.RegisterChannel("/stream/:streamId", pts.ChannelHandlers{
OnSubscribe: func(s *pts.Context) {
println("Client joined: " + s.FullPath)
},
OnMessage: func(s *pts.Context, message *pts.Message) {
println("New Message on " + s.FullPath + ": " + string(message.Payload))
},
OnUnsubscribe: func(s *pts.Context) {
println("Client left: " + s.FullPath)
},
})
- Provide a connect route
r.GET("/connect", func(c *gin.Context) {
properties := make(map[string]interface{}, 1)
properties["ctx"] = c
if err := tubeSystem.HandleRequest(c.Writer, c.Request, properties); err != nil {
println("Something went wrong while handling a Socket request")
}
})
- Connect from a frontend lib
const client = new GoPTSClient({ url: socketUrl, debugging: true })
client.subscribeChannel("test", console.log);
client.send("test", { payload: { foo: "bar" } })
To get a quick overview of how to use Go-PTS, check out the examples
folder.