Skip to content

Commit 633e946

Browse files
committed
Update based on new salesforce generic subscription
1 parent 9f83643 commit 633e946

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# bayeux
22
Bayeux Client Protocol implemented in Golang (as specified by Salesforce Realtime API)
33

4+
Fork from zph/bayeux:
5+
Changes to accept both 'payload' and 'sobject'.
6+
All the channels (user-created or generic) should be accepted.
7+
Make it so user will pass chan from main function then they can close it anytime.
8+
User can metioned replay mechanism from Channel method.
9+
410
# Usage
511
See `examples/main.go`
612
```golang
@@ -9,13 +15,15 @@ package main
915
import (
1016
"fmt"
1117

12-
bay "github.com/zph/bayeux"
18+
bay "github.com/kush-elastic/bayeux"
1319
)
1420

1521
func Example() {
22+
out := make(chan bay.TriggerEvent)
1623
b := bay.Bayeux{}
1724
creds := bay.GetSalesforceCredentials()
18-
c := b.TopicToChannel(creds, "topicName")
25+
replay := "-1"
26+
c := b.Channel(out, replay, creds, "channel")
1927
for {
2028
select {
2129
case e := <-c:

bayeux.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ type TriggerEvent struct {
2525
ReplayID int `json:"replayId"`
2626
Type string `json:"type"`
2727
} `json:"event"`
28-
Object json.RawMessage `json:"sobject"`
28+
Object json.RawMessage `json:"sobject"`
29+
Payload json.RawMessage `json:"payload"`
2930
} `json:"data,omitempty"`
3031
Channel string `json:"channel"`
3132
Successful bool `json:"successful,omitempty"`
3233
}
3334

34-
func (t TriggerEvent) topic() string {
35+
func (t TriggerEvent) channel() string {
3536
s := strings.Replace(t.Channel, "/topic/", "", 1)
3637
return s
3738
}
@@ -157,15 +158,15 @@ type Replay struct {
157158
Value int
158159
}
159160

160-
func (b *Bayeux) subscribe(topic string, replay Replay) Subscription {
161+
func (b *Bayeux) subscribe(channel string, replay string) Subscription {
161162
handshake := fmt.Sprintf(`{
162163
"channel": "/meta/subscribe",
163-
"subscription": "/topic/%s",
164+
"subscription": "%s",
164165
"clientId": "%s",
165166
"ext": {
166-
"replay": {"/topic/%s": "%d"}
167+
"replay": {"%s": "%s"}
167168
}
168-
}`, topic, b.id.clientID, topic, replay)
169+
}`, channel, b.id.clientID, channel, replay)
169170
resp, err := b.call(handshake, b.creds.bayeuxUrl())
170171
if err != nil {
171172
logger.Fatalf("Cannot subscribe %s", err)
@@ -174,7 +175,6 @@ func (b *Bayeux) subscribe(topic string, replay Replay) Subscription {
174175
defer resp.Body.Close()
175176
if os.Getenv("DEBUG") != "" {
176177
logger.Printf("Response: %+v", resp)
177-
// // Read the content
178178
var b []byte
179179
if resp.Body != nil {
180180
b, _ = ioutil.ReadAll(resp.Body)
@@ -199,25 +199,21 @@ func (b *Bayeux) subscribe(topic string, replay Replay) Subscription {
199199
sub := h[0]
200200
status.connected = sub.Successful
201201
status.clientID = sub.ClientID
202-
status.channels = append(status.channels, topic)
202+
status.channels = append(status.channels, channel)
203203
logger.Printf("Established connection(s): %+v", status)
204204
return sub
205205
}
206206

207-
func (b *Bayeux) connect() chan TriggerEvent {
208-
out := make(chan TriggerEvent)
207+
func (b *Bayeux) connect(out chan TriggerEvent) chan TriggerEvent {
209208
go func() {
210-
// TODO: add stop chan to bring this thing to halt
211209
for {
212210
postBody := fmt.Sprintf(`{"channel": "/meta/connect", "connectionType": "long-polling", "clientId": "%s"} `, b.id.clientID)
213211
resp, err := b.call(postBody, b.creds.bayeuxUrl())
214212
if err != nil {
215213
logger.Printf("Cannot connect to bayeux %s", err)
216214
logger.Println("Trying again...")
217215
} else {
218-
defer resp.Body.Close()
219216
if os.Getenv("DEBUG") != "" {
220-
// // Read the content
221217
var b []byte
222218
if resp.Body != nil {
223219
b, _ = ioutil.ReadAll(resp.Body)
@@ -277,15 +273,14 @@ func mustGetEnv(s string) string {
277273
return r
278274
}
279275

280-
func (b *Bayeux) TopicToChannel(creds Credentials, topic string) chan TriggerEvent {
276+
func (b *Bayeux) Channel(out chan TriggerEvent, r string, creds Credentials, channel string) chan TriggerEvent {
281277
b.creds = creds
282278
err := b.getClientID()
283279
if err != nil {
284280
log.Fatal("Unable to get bayeux ClientId")
285281
}
286-
r := Replay{ReplayAll}
287-
b.subscribe(topic, r)
288-
c := b.connect()
282+
b.subscribe(channel, r)
283+
c := b.connect(out)
289284
wg.Add(1)
290285
return c
291286
}

example_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package bayeux
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
func Example() {
8+
out := make(chan TriggerEvent)
9+
replay := "-1"
610
b := Bayeux{}
711
creds := GetSalesforceCredentials()
8-
c := b.TopicToChannel(creds, "topicName")
12+
c := b.Channel(out, replay, creds, "channel")
913
for {
1014
select {
1115
case e := <-c:

examples/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package main
33
import (
44
"fmt"
55

6-
bay "github.com/zph/bayeux"
6+
bay "github.com/kush-elastic/bayeux"
77
)
88

99
func Example() {
10+
out := make(chan bay.TriggerEvent)
1011
b := bay.Bayeux{}
1112
creds := bay.GetSalesforceCredentials()
12-
c := b.TopicToChannel(creds, "topicName")
13+
replay := "-1"
14+
c := b.Channel(out, replay, creds, "channel")
1315
for {
1416
select {
1517
case e := <-c:

0 commit comments

Comments
 (0)