Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 4b463e3

Browse files
committed
added Publish function
1 parent d79786e commit 4b463e3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

cache.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,24 @@ func QueueWork(token, key, value string) error {
7171
var ok bool
7272
return Post(token, "/sudo/cache", data, &ok)
7373
}
74+
75+
// Publish sends a message to a channel (topic) where usually a server-side
76+
// function will process the message.
77+
func Publish(token, channel, typ string, data interface{}) error {
78+
b, err := json.Marshal(data)
79+
if err != nil {
80+
return err
81+
}
82+
83+
payload := new(struct {
84+
Channel string `json:"channel"`
85+
Type string `json:"type"`
86+
Data string `json:"data"`
87+
})
88+
payload.Channel = channel
89+
payload.Type = typ
90+
payload.Data = string(b)
91+
92+
var status bool
93+
return Post(token, "/publish", payload, &status)
94+
}

cache_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package backend_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/staticbackendhq/backend-go"
7+
)
8+
9+
func TestPublishMessage(t *testing.T) {
10+
fakeTask := Task{
11+
Name: "not real",
12+
Done: true,
13+
}
14+
15+
if err := backend.Publish(token, "test-channel", "test-type", fakeTask); err != nil {
16+
t.Fatal(err)
17+
}
18+
}

0 commit comments

Comments
 (0)