Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 25, 2025

Users couldn't find documentation on how to close WebSocket connections. The information existed in callback type signatures but lacked discoverability and examples.

Changes

README.md - Added "Closing Connections" section with:

  • Basic close via {:close, state} return tuple
  • Close with custom code/reason via {:close, {close_code, message}, state}
  • Programmatic close using cast messages
  • WebSocket close code ranges (1000-1015 standard, 3000-3999 registered, 4000-4999 private)

lib/websockex.ex - Added module-level "Closing Connections" documentation for HexDocs visibility

Example

# Basic close
def handle_frame({:text, "shutdown"}, state) do
  {:close, state}
end

# Close with custom code
def handle_frame({:text, "error"}, state) do
  {:close, {4000, "Custom application error"}, state}
end

# Programmatic close
def close(pid), do: WebSockex.cast(pid, :close)

def handle_cast(:close, state), do: {:close, state}

All callbacks (handle_frame/2, handle_cast/2, handle_info/2, handle_ping/2, handle_pong/2) support close return tuples.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https /usr/lib/apt/methods/https (dns block)
  • repo.hex.pm
    • Triggering command: /usr/lib/erlang/erts-13.2.2.5/bin/inet_gethost 4 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>How to shutdown/close websocket?</issue_title>
<issue_description>I couldn't find in the documentation how to close a websocket when it is no longer of any use. Is this possible? Maybe the documentation just needs a small update?</issue_description>

<agent_instructions>Please add the information on how to close the websocket connection to the documentation.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@Azolo Yeah, there's not a specific place that tells that I guess.

The answer is most of the callbacks have the option to return a {:close, state} tuple.

https://hexdocs.pm/websockex/0.4.3/WebSockex.html#c:handle_cast/2</comment_new>
<comment_new>@Azolo
This is definitely a situation where I don't know where to put that particular piece of documentation.

If you have any ideas, let me know.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add documentation on closing websocket connections Add documentation for closing WebSocket connections Nov 25, 2025
Copilot AI requested a review from dominicletz November 25, 2025 21:32
@dominicletz dominicletz marked this pull request as ready for review November 30, 2025 13:51
@dominicletz dominicletz merged commit d5684d4 into master Nov 30, 2025
3 checks passed
@dominicletz dominicletz deleted the copilot/update-websocket-closure-docs branch November 30, 2025 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to shutdown/close websocket?

2 participants