Skip to content

Go

Terminal window
go get github.com/devteapot/slop/packages/go/slop-ai

The Go SDK uses the standard library plus nhooyr.io/websocket for WebSocket support.

package main
import (
"context"
"log"
"net/http"
slop "github.com/devteapot/slop/packages/go/slop-ai"
)
func main() {
server := slop.NewServer("my-api", "My API")
server.Register("status", slop.Node{
Type: "status",
Props: slop.Props{"healthy": true},
})
server.Handle("status", "restart", slop.HandlerFunc(
func(ctx context.Context, params slop.Params) (any, error) {
return map[string]any{"ok": true}, nil
},
))
server.Mount(http.DefaultServeMux)
log.Fatal(http.ListenAndServe(":8080", nil))
}

server.Mount() adds:

  • GET /slop for the WebSocket endpoint
  • GET /.well-known/slop for provider discovery

Use RegisterFunc() for state that should be re-read on every refresh:

server.RegisterFunc("metrics", func() slop.Node {
return slop.Node{
Type: "status",
Props: slop.Props{"requests": getRequestCount()},
}
})

Call server.Refresh() after mutations outside SLOP actions.

For local apps and CLI tools:

ctx := context.Background()
go slop.ListenUnix(ctx, server, "/tmp/slop/my-app.sock", slop.WithDiscovery(true))
go slop.ListenStdio(ctx, server)

Use WithDiscovery(true) to write ~/.slop/providers/<id>.json for local discovery.

transport := &slop.WSClientTransport{URL: "ws://localhost:8080/slop"}
consumer := slop.NewConsumer(transport)
hello, _ := consumer.Connect(context.Background())
subID, snapshot, _ := consumer.Subscribe(context.Background(), "/", -1)
_, _ = consumer.Invoke(context.Background(), "/status", "restart", nil)
fmt.Println(hello["provider"], subID, snapshot.ID)

The Go SDK also includes the core discovery layer in the discovery subpackage:

import (
"context"
"fmt"
"github.com/devteapot/slop/packages/go/slop-ai/discovery"
)
svc := discovery.NewService(discovery.ServiceOptions{})
svc.Start(context.Background())
defer svc.Stop()
provider, err := svc.EnsureConnected(context.Background(), "my-app")
if err != nil {
panic(err)
}
if provider != nil {
fmt.Println(provider.Name)
}