Skip to content
/ go-pts Public

Go Library for WebSocket Channel Management ✨

License

Notifications You must be signed in to change notification settings

mono424/go-pts

Repository files navigation


Go (Pneumatic) Tube System

Run Tests Go Report Card codecov

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.

Installation

Installing the main library

  1. Get the go-pts package using the following command:
go get github.com/mono424/go-pts

Using connectors

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"
)

Client Libraries

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

Connectors

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

Getting Started

  1. Create a new TubeSystem
tubeSystem := pts.New(ptsc_gorilla.NewConnector(
  websocket.Upgrader{},
  func(err *pts.Error) {
    println(err.Description)
  },
))
  1. 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)
  },
})
  1. 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")
  }
})
  1. Connect from a frontend lib
const client = new GoPTSClient({ url: socketUrl, debugging: true })
client.subscribeChannel("test", console.log);
client.send("test", { payload: { foo: "bar" } })

Examples

To get a quick overview of how to use Go-PTS, check out the examples folder.