Skip to content

Feature Request: Add close_output/2 for Half-Close WebSocket Support #140

@chgeuer

Description

@chgeuer

Summary

Add support for closing only the output side of a WebSocket connection while continuing to receive data from the server. This enables the "half-close" pattern where a client signals it's done sending but
remains open to receive remaining server messages.

Motivation

Some WebSocket-based applications require the client to signal completion of sending data while still needing to receive responses or acknowledgments from the server. Currently, WebSockex.close/2 initiates a
full close handshake that terminates the entire connection.

The half-close pattern allows:

  • Client sends a close frame to indicate "I'm done sending"
  • Client continues receiving frames until the server sends its close frame
  • Connection terminates cleanly after both sides have closed

Proposed API

# Close output with default close frame
WebSockex.close_output(pid)

# Close output with custom close code and message
WebSockex.close_output(pid, {1000, "Done sending"})

Expected Behavior

  • close_output/2 sends a WebSocket close frame but keeps the connection open for receiving
  • After calling close_output, send_frame/2 returns {:error, %WebSockex.NotConnectedError{connection_state: :output_closed}}
  • The client continues to receive and process incoming frames via callbacks (handle_frame, etc.)
  • When the server responds with its close frame, the connection terminates normally
  • Callbacks that try to reply with a frame have their replies silently ignored (logged for debugging)

Use Case

Applications where the client streams data to a server, then waits for the server to finish processing and send final results before disconnecting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions