https://github.com/bruth/go-pulsar-ws
Go client for Apache Pulsar using the Websocket protocol.
https://github.com/bruth/go-pulsar-ws
Last synced: about 1 year ago
JSON representation
Go client for Apache Pulsar using the Websocket protocol.
- Host: GitHub
- URL: https://github.com/bruth/go-pulsar-ws
- Owner: bruth
- License: mit
- Created: 2018-05-07T13:23:43.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-07T13:53:38.000Z (about 8 years ago)
- Last Synced: 2025-03-20T00:51:30.651Z (over 1 year ago)
- Language: Go
- Size: 5.86 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-pulsar-ws
[](http://godoc.org/github.com/bruth/go-pulsar-ws)
A Go client for [Apache Pulsar](https://pulsar.incubator.apache.org) using the [Websocket protocol](https://pulsar.incubator.apache.org/docs/latest/clients/WebSocket/).
## Status
Currently, there is no official Go client using the [binary protocol], although the Pulsar team is working on a cgo-based implementation. This library was created as a temporary solution. Once an official solution comes out, I will likely not support this anymore.
## Usage
### Producer
```go
// Initialize a client passing the Pulsar Websocket endpoint.
client := pulsar.New("ws://localhost:8080/ws")
// Initialize a producer given a topic with an optional set of parameters.
// Establishes a websocket connection.
producer, err := client.Producer("persistent/standalone/us-east/test", nil)
if err != nil {
log.Fatal(err)
}
defer producer.Close()
ctx := context.Background()
res, err := producer.Send(ctx, *pulsar.PublishMsg{
Payload: []byte("hello world!"),
})
if err != nil {
log.Fatal(err)
}
// Print the resulting message id.
log.Print(res.MsgId)
```
### Consumer
```go
client := pulsar.New("ws://localhost:8080/ws")
consumer, err := client.Consumer("persistent/standalone/us-east/test", "my-sub", nil)
if err != nil {
log.Fatal(err)
}
defer consumer.Close()
ctx := context.Background()
for {
msg, err := consumer.Receive(ctx)
if err != nil {
log.Fatal(err)
}
// Print message.
log.Print(string(msg.Payload))
// Ack once processed.
if err := consumer.Ack(ctx, msg); err != nil {
log.Fatal(err)
}
}
```
### Reader
```go
client := pulsar.New("ws://localhost:8080/ws")
reader, err := client.Reader("persistent/standalone/us-east/test", pulsar.Params{
"messageId": "earliest",
})
if err != nil {
log.Fatal(err)
}
defer reader.Close()
ctx := context.Background()
for {
msg, err := reader.Receive(ctx)
if err != nil {
log.Fatal(err)
}
// Print message.
log.Print(string(msg.Payload))
// Ack once processed.
if err := reader.Ack(ctx, msg); err != nil {
log.Fatal(err)
}
}
```
## License
MIT