Using the Grav Instance
Once you have a Grav instance, you can start connecting Pods and sending messages!
1
package main
2
3
import (
4
"fmt"
5
"time"
6
7
"github.com/suborbital/grav/grav"
8
)
9
10
func useGravInstance() {
11
g := grav.New()
12
13
// create a pod and set a function to be called on each message
14
p := g.Connect()
15
p.On(func(msg grav.Message) error {
16
fmt.Println("message received:", string(msg.Data()))
17
18
return nil
19
})
20
21
// create a second pod and send a message
22
p2 := g.Connect()
23
p2.Send(grav.NewMsg(grav.MsgTypeDefault, []byte("hello, world")))
24
25
// messages are asyncronous, so pause for a second to allow the message to send
26
<-time.After(time.Second)
27
}
28
Copied!
Here we can see Pods being created, and the two main Pod methods being used. Read about Pods if you haven't already.
Each time On is called, the function used to receive messages gets replaced, therefore if multiple receiving functions are needed, multiple Pods should be created.
Last modified 1yr ago
Copy link