Skip to content

Commit

Permalink
Add OnSubscribe, OnUnsubscribe events examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mochi committed May 4, 2022
1 parent 27f3c48 commit bef13ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely sim
- Interfaces for Client Authentication and Topic access control.
- Bolt persistence and storage interfaces (see examples folder).
- Directly Publishing from embedding service (`s.Publish(topic, message, retain)`).
- Basic Event Hooks (`OnMessage`, `OnConnect`, `OnDisconnect`, `onProcessMessage`, `OnError`, `OnStorage`).
- Basic Event Hooks (`OnMessage`, `onSubscribe`, `onUnsubscribe`, `OnConnect`, `OnDisconnect`, `onProcessMessage`, `OnError`, `OnStorage`).
- ARM32 Compatible.

#### Roadmap
Expand Down Expand Up @@ -141,7 +141,25 @@ server.Events.OnMessage = func(cl events.Client, pk events.Packet) (pkx events.P

```go
server.Events.OnDisconnect = func(cl events.Client, err error) {
fmt.Printf("<< OnDisconnect client dicconnected %s: %v\n", cl.ID, err)
fmt.Printf("<< OnDisconnect client disconnected %s: %v\n", cl.ID, err)
}
```

##### OnSubscribe
`server.Events.OnSubscribe` is called when a client subscribes to a new topic filter.

```go
server.Events.OnSubscribe = func(filter string, cl events.Client, qos byte) {
fmt.Printf("<< OnSubscribe client subscribed %s: %s %v\n", cl.ID, filter, qos)
}
```

##### OnUnsubscribe
`server.Events.OnUnsubscribe` is called when a client unsubscribes from a topic filter.

```go
server.Events.OnUnsubscribe = func(filter string, cl events.Client) {
fmt.Printf("<< OnUnsubscribe client unsubscribed %s: %s\n", cl.ID, filter)
}
```

Expand All @@ -158,7 +176,6 @@ If an error is returned, the packet will not be modified. and the existing packe

> This hook is only triggered when a message is received by clients. It is not triggered when using the direct `server.Publish` method.

```go
import "github.com/mochi-co/mqtt/server/events"

Expand All @@ -175,6 +192,8 @@ server.Events.OnMessage = func(cl events.Client, pk events.Packet) (pkx events.P

The OnMessage hook can also be used to selectively only deliver messages to one or more clients based on their id, using the `AllowClients []string` field on the packet structure.



##### OnError
`server.Events.OnError` is called when an error is encountered on the server, particularly within the use of a client connection status.

Expand Down
10 changes: 10 additions & 0 deletions examples/events/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func main() {
fmt.Printf("<< OnDisconnect client disconnected %s: %v\n", cl.ID, err)
}

// Add OnSubscribe Event Hook
server.Events.OnSubscribe = func(filter string, cl events.Client, qos byte) {
fmt.Printf("<< OnSubscribe client subscribed %s: %s %v\n", cl.ID, filter, qos)
}

// Add OnUnsubscribe Event Hook
server.Events.OnUnsubscribe = func(filter string, cl events.Client) {
fmt.Printf("<< OnUnsubscribe client unsubscribed %s: %s\n", cl.ID, filter)
}

// Add OnMessage Event Hook
server.Events.OnMessage = func(cl events.Client, pk events.Packet) (pkx events.Packet, err error) {
pkx = pk
Expand Down

0 comments on commit bef13ee

Please sign in to comment.