Help us shape the future of Go! Take the 2026 Go Developer Survey

client

package
v1.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package client provides a Go client for connecting to a remote Dispatch instance via the Dispatch Wire Protocol (DWP) over WebSocket.

Usage:

c, err := client.Dial("wss://api.example.com/dwp",
    client.WithToken("dk_..."),
)
defer c.Close()

// Enqueue a job.
job, err := c.Enqueue(ctx, "send-email", payload)

// Start a workflow and watch events.
run, err := c.StartWorkflow(ctx, "order-pipeline", input)
ch := c.Watch(ctx, run.RunID)
for evt := range ch {
    fmt.Printf("Step %s: %s\n", evt.StepName, evt.Type)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a DWP client that communicates with a remote Dispatch server.

func Dial

func Dial(url string, opts ...Option) (*Client, error)

Dial connects to a DWP server and authenticates.

func DialContext

func DialContext(ctx context.Context, url string, opts ...Option) (*Client, error)

DialContext connects to a DWP server with a context.

func (*Client) CancelJob

func (c *Client) CancelJob(ctx context.Context, jobID string) error

CancelJob cancels a job by ID.

func (*Client) Close

func (c *Client) Close() error

Close closes the client connection.

func (*Client) Enqueue

func (c *Client) Enqueue(ctx context.Context, name string, payload any, opts ...EnqueueOption) (*JobResult, error)

Enqueue submits a job to the remote dispatch server.

func (*Client) GetJob

func (c *Client) GetJob(ctx context.Context, jobID string) (json.RawMessage, error)

GetJob retrieves a job by ID.

func (*Client) GetWorkflow

func (c *Client) GetWorkflow(ctx context.Context, runID string) (json.RawMessage, error)

GetWorkflow retrieves a workflow run by ID.

func (*Client) PublishEvent

func (c *Client) PublishEvent(ctx context.Context, name string, payload any) error

PublishEvent publishes an event that can trigger workflow continuations.

func (*Client) SessionID

func (c *Client) SessionID() string

SessionID returns the session ID assigned by the server.

func (*Client) StartWorkflow

func (c *Client) StartWorkflow(ctx context.Context, name string, input any) (*WorkflowResult, error)

StartWorkflow starts a workflow on the remote dispatch server.

func (*Client) Stats

func (c *Client) Stats(ctx context.Context) (json.RawMessage, error)

Stats retrieves broker and connection statistics from the server.

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, channel string) (<-chan *stream.Event, error)

Subscribe subscribes to a stream topic and returns a channel of events. The channel is closed when the client disconnects or Unsubscribe is called.

Topics follow the dispatch stream convention:

  • "job:<jobID>" — Events for a specific job
  • "workflow:<runID>" — Step-by-step events for a workflow run
  • "queue:<name>" — All events for a queue
  • "jobs" — All job lifecycle events
  • "workflows" — All workflow lifecycle events
  • "firehose" — Everything

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(ctx context.Context, channel string) error

Unsubscribe removes a subscription.

func (*Client) Watch

func (c *Client) Watch(ctx context.Context, runID string) (<-chan *stream.Event, error)

Watch subscribes to workflow events for a specific run and returns an event channel. This is a convenience method that subscribes to "workflow:<runID>".

type EnqueueOption

type EnqueueOption func(*dwp.JobEnqueueRequest)

EnqueueOption configures an enqueue request.

func WithPriority

func WithPriority(priority int) EnqueueOption

WithPriority sets the job priority.

func WithQueue

func WithQueue(queue string) EnqueueOption

WithQueue sets the target queue.

type JobResult

type JobResult struct {
	JobID string `json:"job_id"`
	Queue string `json:"queue"`
	State string `json:"state"`
}

JobResult contains the result of an enqueue operation.

type Option

type Option func(*Client)

Option configures a Client.

func WithFormat

func WithFormat(format string) Option

WithFormat sets the wire format for frame encoding. Supported values: "json" (default), "msgpack".

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger sets the structured logger.

func WithReconnect

func WithReconnect(maxRetries int, baseDelay time.Duration) Option

WithReconnect enables automatic reconnection with the given parameters.

func WithToken

func WithToken(token string) Option

WithToken sets the authentication token.

type WorkflowResult

type WorkflowResult struct {
	RunID string `json:"run_id"`
	Name  string `json:"name"`
	State string `json:"state"`
}

WorkflowResult contains the result of starting a workflow.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL