/gulsar

Pulsar WebSocket Client Library for Go.

Primary LanguageGoMIT LicenseMIT

gulsar License: MIT release CircleCI codecov Codacy Badge Go Report Card GoDoc Join the chat at https://gitter.im/kpango/gulsar

Pulsar WebSocket Client Library for Go.

Requirement

Go 1.8

Installation

go get github.com/kpango/gulsar

Example

package main

import (
	"context"
	"time"

	"github.com/kpango/glg"
	"github.com/kpango/gulsar"
)

func main() {
	g := gulsar.New("http://localhost", "wss://pulsar:port/path/topic")
	g.SetTlsCredential("sample.cert", "sample.key")

	ctx, cancel := context.WithCancel(context.Background())

	defer cancel()

	g.ConnectWithHeader(ctx, map[string][]string{
		"Auth-Header-Key": {
			"Token",
		},
	})

	ticker := time.NewTicker(time.Second * 3)
	go func() {
		for {
			select {
			case <-ctx.Done():
				return
			case <-ticker.C:
				err := g.Produce(&gulsar.ProduceMessage{})
				if err != nil {
					glg.Error(err)
					continue
				}

				ackMsg, err := g.ReceiveACK()
				if err != nil {
					glg.Error(err)
					continue
				}

				glg.Info(ackMsg)
			}
		}
	}()

	for {
		select {
		case <-ctx.Done():
			return
		default:
			msg, err := g.Consume()
			if err != nil {
				glg.Error(err)
				continue
			}

			glg.Info(string(msg.Body))

			g.SendACK(msg.MessageID)

		}
	}
}

Contribution

  1. Fork it ( https://github.com/kpango/gulsar/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author

kpango

LICENSE

gulsar released under MIT license, refer LICENSE file.