ws

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package ws provides a WebSocket client for real-time bidirectional communication between the NerdBackup agent and the platform.

This replaces 5-minute polling with instant command delivery and progress streaming. Falls back to HTTP polling when the WebSocket connection is unavailable.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotConnected = errors.New("websocket: not connected")

ErrNotConnected is returned when attempting to send a message while the WebSocket connection is not established.

Functions

This section is empty.

Types

type Client

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

Client manages the WebSocket connection to the NerdBackup server.

func NewClient

func NewClient(apiURL, agentID, token string, onCommand func(Command)) *Client

NewClient creates a new WebSocket client.

apiURL is the base NerdBackup API URL (e.g., "https://nerdbackup.com"). The client converts it to the WebSocket URL automatically. onCommand is called for each command received from the server.

func (*Client) Close

func (c *Client) Close()

Close cleanly shuts down the WebSocket connection.

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected returns whether the WebSocket is currently connected.

func (*Client) Run

func (c *Client) Run(ctx context.Context)

Run starts the WebSocket connection with automatic reconnection. It blocks until ctx is cancelled. Use this in a goroutine.

Reconnection uses exponential backoff from 1s to 60s with 25% jitter.

func (*Client) Send

func (c *Client) Send(msg Message) error

Send sends a JSON message to the server.

func (*Client) SendHeartbeat

func (c *Client) SendHeartbeat(data HeartbeatData) error

SendHeartbeat sends a heartbeat message over WebSocket.

func (*Client) SendJobReport

func (c *Client) SendJobReport(data interface{}) error

SendJobReport sends a completed/failed job report over WebSocket.

func (*Client) SendProgress

func (c *Client) SendProgress(progress ProgressData) error

SendProgress sends real-time backup progress over WebSocket.

type Command

type Command struct {
	Type   string          `json:"type"`
	Action string          `json:"action"` // start_backup, pause, cancel, resume, config_update
	JobID  string          `json:"job_id,omitempty"`
	Data   json.RawMessage `json:"data,omitempty"`
}

Command represents a server-to-agent message received over WebSocket.

type HeartbeatData

type HeartbeatData struct {
	AgentVersion  string `json:"agent_version"`
	ResticVersion string `json:"restic_version"`
	Platform      string `json:"platform"`
	Arch          string `json:"arch"`
	Hostname      string `json:"hostname"`
	UptimeSeconds int64  `json:"uptime_seconds"`
	DiskFreeBytes int64  `json:"disk_free_bytes"`
	CPUCount      int    `json:"cpu_count"`
	MemTotalBytes int64  `json:"memory_total_bytes"`
}

HeartbeatData holds agent heartbeat information sent over WebSocket.

type Message

type Message struct {
	Type string      `json:"type"` // heartbeat, progress, job_report
	Data interface{} `json:"data"`
}

Message represents an agent-to-server message sent over WebSocket.

type ProgressData

type ProgressData struct {
	RepoID         string  `json:"repo_id"`
	JobID          string  `json:"job_id,omitempty"`
	PercentDone    float64 `json:"percent_done"`
	BytesProcessed int64   `json:"bytes_processed"`
	FilesProcessed int     `json:"files_processed"`
	CurrentFile    string  `json:"current_file,omitempty"`
	StartedAt      string  `json:"started_at"`
}

ProgressData holds real-time backup progress for streaming to the server.

Jump to

Keyboard shortcuts

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