Skip to main content

Examples

Code snippets demonstrating common Spine patterns across Go, Python, and C++.

Basic pub/sub

node := spine.NewNode("publisher")

node.Subscribe("sensor/data", func(msg spine.Msg) {
fmt.Println("received:", string(msg.Payload))
})

node.Publish("sensor/data", []byte("hello"))
node.Start()

RPC service call

// Server node
server := spine.NewNode("arm-controller")
server.Handle("arm/move", func(req spine.Msg) spine.Msg {
target := parseTarget(req.Payload)
angles := kinematics.Solve(target)
return spine.Msg{Payload: mad.Encode(angles)}
})
server.Start()

// Client node
client := spine.NewNode("ui")
resp, err := client.Call("arm/move", mad.Encode(target))

Multi-node with mDNS discovery

Nodes discover each other automatically — no IP addresses or config files needed.

// Run these on any machines on the same network
nodeA := spine.NewNode("purifier")
nodeB := spine.NewNode("controller")

// nodeB will discover nodeA automatically via mDNS
nodeB.Subscribe("input/clean", func(msg spine.Msg) {
// handle filtered input from purifier
})

nodeA.Start()
nodeB.Start()

Encrypted namespace

Nodes sharing a namespace key communicate in an AES-GCM encrypted channel. Nodes without the key cannot read or publish to that namespace.

node := spine.NewNode("purifier",
spine.WithNamespace("surgical-session-001"),
spine.WithKey([]byte("32-byte-aes-key-goes-here-000000")),
)
node.Start()

See also