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 ¶
- Variables
- type Client
- func (c *Client) Close()
- func (c *Client) IsConnected() bool
- func (c *Client) Run(ctx context.Context)
- func (c *Client) Send(msg Message) error
- func (c *Client) SendHeartbeat(data HeartbeatData) error
- func (c *Client) SendJobReport(data interface{}) error
- func (c *Client) SendProgress(progress ProgressData) error
- type Command
- type HeartbeatData
- type Message
- type ProgressData
Constants ¶
This section is empty.
Variables ¶
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 ¶
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) IsConnected ¶
IsConnected returns whether the WebSocket is currently connected.
func (*Client) Run ¶
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) SendHeartbeat ¶
func (c *Client) SendHeartbeat(data HeartbeatData) error
SendHeartbeat sends a heartbeat message over WebSocket.
func (*Client) SendJobReport ¶
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.