fetch supports WebSocket connections for real-time bidirectional communication.
Connect using ws:// or wss:// URL schemes:
fetch ws://echo.websocket.events
fetch wss://echo.websocket.eventsUse -d or -j to send a single message on connect:
fetch ws://echo.websocket.events -d "hello"
fetch ws://echo.websocket.events -j '{"type": "subscribe", "channel": "updates"}'Pipe lines from stdin — each line is sent as a separate text message:
echo "hello" | fetch ws://echo.websocket.events
printf "msg1\nmsg2\n" | fetch ws://echo.websocket.eventsWhen stdin is not piped (i.e. running from a terminal), fetch operates in read-only mode — it listens for server messages until the connection closes or Ctrl+C is pressed.
- Text messages: Written to stdout. JSON messages are automatically formatted when connected to a terminal.
- Binary messages: A
[binary N bytes]indicator is printed to stderr. - Formatting: Use
--format onto force JSON formatting, or--format offto disable it.
# Force JSON formatting
fetch ws://api.example.com/stream --format on
# Disable formatting
fetch ws://api.example.com/stream --format offUse -v flags to see connection details:
# Show response status and headers
fetch -v ws://echo.websocket.events -d "hello"
# Show request and response headers with prefixes
fetch -vv ws://echo.websocket.events -d "hello"All authentication options work with WebSocket connections — headers are sent during the HTTP upgrade handshake:
fetch --bearer mytoken ws://api.example.com/ws
fetch --basic user:pass ws://api.example.com/ws
fetch -H "Authorization: Bearer mytoken" ws://api.example.com/wsSpecify WebSocket subprotocols via the Sec-WebSocket-Protocol header:
fetch -H "Sec-WebSocket-Protocol: graphql-ws" wss://api.example.com/graphqlThe --timeout flag applies to the WebSocket handshake only. The connection stays open until the server closes or stdin EOF:
fetch --timeout 5 ws://api.example.com/ws- WebSocket requires HTTP/1.1 for the upgrade handshake. Using
--http 3with WebSocket is not supported. - WebSocket (
ws:///wss://) cannot be combined with--grpc,--form,--multipart,--xml, or--edit. - Binary message content is not displayed; only a size indicator is shown.
- The pager is disabled for WebSocket output.