Skip to content

Commit

Permalink
Add support for QoS-2
Browse files Browse the repository at this point in the history
Signed-off-by: Chaitanya Munukutla <[email protected]>
  • Loading branch information
c16a committed Apr 6, 2024
1 parent e63a645 commit 2a9852a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A tiny Websocket-based event broker.
- [x] Grouped subscriptions
- [ ] Offline messages
- [ ] Clustering
- [x] QoS (0, 1)
- [x] QoS (0, 1, 2)
- [x] Topics & Patterns

### Subscriptions
Expand Down
8 changes: 8 additions & 0 deletions events/pubcomp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package events

const PubComp = "pubcomp"

type PubCompEvent struct {
Kind string `json:"kind"`
PacketId string `json:"packet_id"`
}
8 changes: 8 additions & 0 deletions events/pubrec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package events

const PubRec = "pubrec"

type PubRecEvent struct {
Kind string `json:"kind"`
PacketId string `json:"packet_id"`
}
8 changes: 8 additions & 0 deletions events/pubrel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package events

const PubRel = "pubrel"

type PubRelEvent struct {
Kind string `json:"kind"`
PacketId string `json:"packet_id"`
}
2 changes: 2 additions & 0 deletions handlers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ func HandleMessage(client *broker.ConnectedClient, broker *broker.Broker, messag
switch kind {
case events.Pub:
return handlePublish(message, client, broker)
case events.PubRel:
return handlePubrel(message, client)
case events.Sub:
return handleSubscribe(message, client)
case events.Unsub:
Expand Down
7 changes: 7 additions & 0 deletions handlers/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func handlePublish(message []byte, client *broker.ConnectedClient, broker *broke
}
client.WriteInterface(pubAckEvent)
}
if event.QoS == 2 {
pubRecEvent := &events.PubRecEvent{
Kind: events.PubRec,
PacketId: uuid.NewString(),
}
client.WriteInterface(pubRecEvent)
}
go broker.Broadcast(event)
return nil
}
21 changes: 21 additions & 0 deletions handlers/pubrel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package handlers

import (
"encoding/json"
"github.com/c16a/microq/broker"
"github.com/c16a/microq/events"
)

func handlePubrel(message []byte, client *broker.ConnectedClient) error {
var event events.PubRelEvent
err := json.Unmarshal(message, &event)
if err != nil {
return err
}
pubCompEvent := &events.PubCompEvent{
Kind: events.PubComp,
PacketId: event.PacketId,
}
client.WriteInterface(pubCompEvent)
return nil
}

0 comments on commit 2a9852a

Please sign in to comment.