Terminate websocket connection in Phoenix channel

Within a Phoenix Channel callback I want to close the websocket connection to a client, preferably with a custom websocket close code (such as 4000, 4006 etc.) How can I do this?

This is what I have tried so far:

  1. By implementing the id() callback and using this technique (broadcsting “disconnect”) Phoenix.Socket — Phoenix v1.6.11. However I want to close with code 4006 such that the javascript socket client for channels will try to reconnect. With this technique, I have checked that the connection is closed with code 4000 and the client library won’t reconnect.
  2. By killing the transport process Process.exit(socket.transport_pid, :kill) but this will log an error.

Is there a way to terminate the transport (which I set to :websocket) cleanly with specified websocket code 4006 from within a channel callback? I have tried to use Process.exit(socket.transport_pid, :normal) but this seems to have no effect.