ratchetphp/Ratchet · error · Exception

Invalid WAMP message type

Error message

Invalid WAMP message type

What it means

This is the `default` branch of the message-type switch in ServerProtocol::onMessage: any frame whose element 0 is not one of WAMP's client-to-server types (1 PREFIX, 2 CALL, 5 SUBSCRIBE, 6 UNSUBSCRIBE, 7 PUBLISH) reaches it. Server-only types (WELCOME=0, CALL RESULT=3, CALL ERROR=4, EVENT=8) and unknown type codes fall through here. The library throws because it cannot dispatch a message type it never expects to receive from a client.

Solutions

  1. Verify the client only sends client-to-server WAMP v1 types: 1 (PREFIX), 2 (CALL), 5 (SUBSCRIBE), 6 (UNSUBSCRIBE), 7 (PUBLISH).
  2. Check for WAMP v2 type codes (e.g. 16 PUBLISH, 32 SUBSCRIBE from newer autobahn clients) — Ratchet implements WAMP v1; use a v1-compatible client or a WAMP v2 router.
  3. Log $json[0] in onError when this exception surfaces to identify the offending type and client.
  4. Catch \Ratchet\Wamp\Exception in your component's onError and close the connection instead of letting it poison the loop.

Example fix

// before (client)
ws.send(JSON.stringify([16, {}, 'kittens', 'hello'])); // WAMP v2 PUBLISH
// after (WAMP v1)
ws.send(JSON.stringify([7, 'kittens', 'hello']));
Defensive patterns

Strategy: validation

Validate before calling

$frame = json_decode($msg, true);
$validClientTypes = [1, 2, 5, 6, 7]; // PREFIX, CALL, SUBSCRIBE, UNSUBSCRIBE, PUBLISH
if (!isset($frame[0]) || !in_array($frame[0], $validClientTypes, true)) {
    // reject: not a valid client-to-server WAMP v1 message type
}

Type guard

function isClientToServerWampType($type): bool {
    return in_array($type, [1, 2, 5, 6, 7], true);
}

Try / catch

try {
    $server->onMessage($conn, $msg);
} catch (\Ratchet\Wamp\Exception $e) {
    error_log("Unknown WAMP type from {$conn->remoteAddress}: " . $msg);
    $conn->close();
}

Prevention

When it happens

Trigger: A client sends [0, ...] (WELCOME), [3, ...], [4, ...], [8, ...] — messages that are server-to-client only — or a completely unknown type code like [99, "x"], or an empty array [] where $json[0] is undefined.

Common situations: A developer echoing back or relaying server frames into the same socket; test scripts replaying captured server messages; clients speaking WAMP v2 (whose type codes like SUBSCRIBE=32, PUBLISH=16 differ) so codes land on undefined switch values; buggy message routing between connections.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of ratchetphp/Ratchet@e621c6c40b (2026-09-16). Data as JSON: /api/errors/9647a15d1e2d504f. Report an issue: GitHub.

Appendix: source

Thrown at src/Ratchet/Wamp/ServerProtocol.php:141

            break;

            case static::MSG_PUBLISH:
                $exclude  = (array_key_exists(3, $json) ? $json[3] : null);
                if (!is_array($exclude)) {
                    if (true === (bool)$exclude) {
                        $exclude = [$from->WAMP->sessionId];
                    } else {
                        $exclude = [];
                    }
                }

                $eligible = (array_key_exists(4, $json) ? $json[4] : []);

                $this->_decorating->onPublish($from, $from->getUri($json[1]), $json[2], $exclude, $eligible);
            break;

            default:
                throw new Exception('Invalid WAMP message type');
        }
    }

    /**
     * {@inheritdoc}
     */
    public function onClose(ConnectionInterface $conn) {
        $decor = $this->connections[$conn];
        $this->connections->offsetUnset($conn);

        $this->_decorating->onClose($decor);
    }

    /**
     * {@inheritdoc}
     */
    public function onError(ConnectionInterface $conn, \Exception $e) {
        return $this->_decorating->onError($this->connections[$conn], $e);

View on GitHub (pinned to e621c6c40b)