Skip to content

Mattermost WebSocket: add ping/pong keepalive to detect silent connection drops #44160

Description

@allenliang2022

Problem

The Mattermost plugin's WebSocket connection has no ping/pong heartbeat mechanism. When the TCP connection dies silently (e.g. NAT table expiry, intermediate router drops), no close or error event fires on the WebSocket. The runWithReconnect loop never triggers because it depends on these events.

Result: The bot appears online in OpenClaw but is actually disconnected from Mattermost indefinitely. Messages are silently lost until a manual gateway restart.

Root Cause

In extensions/mattermost/src/mattermost/monitor-websocket.ts, createMattermostConnectOnce only listens for open, message, close, and error events. There is no periodic ping/pong or application-level keepalive.

This is a well-known WebSocket issue — TCP keepalive defaults are too long (often 2+ hours) to be useful, and many NAT/proxy layers drop idle connections much sooner.

Suggested Fix

Add a periodic WebSocket ping with a pong timeout. Something like:

const PING_INTERVAL_MS = 30_000;  // ping every 30s
const PONG_TIMEOUT_MS = 10_000;   // expect pong within 10s

const pingTimer = setInterval(() => {
  if (ws.readyState === WebSocket.OPEN) {
    let pongReceived = false;
    ws.once("pong", () => { pongReceived = true; });
    ws.ping();
    setTimeout(() => {
      if (!pongReceived && ws.readyState === WebSocket.OPEN) {
        runtime.error?.("mattermost: pong timeout, closing connection");
        ws.terminate();  // triggers close → reconnect kicks in
      }
    }, PONG_TIMEOUT_MS);
  }
}, PING_INTERVAL_MS);

Clean up the timer on close/error.

This ensures runWithReconnect (which already works well) gets a chance to fire when the connection is actually dead.

Environment

  • OpenClaw: latest (npm)
  • Mattermost: Docker self-hosted
  • Network: behind NAT + Caddy reverse proxy
  • Observed: WebSocket silently died after ~2 days of uptime, bot status showed "offline" in Mattermost but gateway process was still running

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions