phxsocket
Synchronous phoenix websocket client using callbacks
Requirements
websockets
Usage
Import the package
import phxsocket
Create socket client
# endpoint.ex
socket "/socket", MyAppWeb.Socket, websocket: true, longpoll: false
socket = phxsocket.Client("wss://target.url/socket/websocket", {"name": "my name"})
Socket params go to Phoenix.Socket connect/3
Connect and join a channel
# mysocket.ex
defmodule MyAppWeb.Socket do
use Phoenix.Socket
channel("channel:*", MyAppWeb.Channel)
def connect(%{"name" => name} = params, socket, _connect_info) do
{:ok, socket |> assign(name: name)}
end
def id(socket), do: nil
end
defmodule MyAppWeb.Channel do
use Phoenix.Channel
def join("channel:" <> room_name, %{"password" => password}, socket) do
if password == "1234" do
{:ok, socket}
else
{:error, %{reason: "unauthorized"}}
end
end
end
if socket.connect(): # blocking, raises exception on failure
channel = socket.channel("channel:my room", {"password": "1234"})
resp = channel.join() # also blocking, raises exception on failure
Alternatively
def connect_to_channel(socket):
channel = socket.channel("channel:my room", {"password": "1234"})
resp = channel.join()
socket.on_open = connect_to_channel
connection = socket.connect(blocking=False)
connection.wait() # blocking, raises exception on failure
Reconnect on disconnection
socket.on_close = lambda socket: socket.connect()
Subscribe to events
def do_something(payload):
content = payload["content"]
channel.on("message", do_something)
MyAppWeb.Endpoint.broadcast("channel:my room", "message", %{"content": "hello"})
Push data to a channel
defmodule MyAppWeb.Channel do
...
def handle_in("message", %{"content" => content}, socket) do
IO.inspect("received from #{socket.assigns.name}: #{content}")
{:reply, {:ok, "hello"}, socket}
end
channel.push("message", {"content": "hello"})
This throws away the reply if the return value of handle_in is :reply
Push data and wait for a response
message = channel.push("message", {"content": "hello"}, reply=True)
payload = message.wait_for_response() # blocking
Push data and react to the response with a callback
def response(payload):
print(payload["status"]) # ok
print(payload["response"]) # hello
channel.push("message", {"content": "hello"}, response)
Leave a channel
channel.leave()
Disconnect
socket.close()
Metadata
Release files for phxsocket 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| phxsocket-0.1.5.tar.gz | 17.3 kB | Without provenance | Details for phxsocket-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| phxsocket-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for phxsocket-0.1.5-py3-none-any.whl |
Total release size: 35.3 kB
Release files / phxsocket-0.1.5.tar.gz
| Download URL | phxsocket-0.1.5.tar.gz |
|---|---|
| Size | 17.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ab4dba9cf5a25f6ae2c1959ca6c6e2da0a6d026f546aed6d42cbab8e7bb6c36c
|
|
BLAKE2b-256 checksum How to use checksums |
360f90a2060e1ed9dfcf07bf8d9be69f0f9dcdd6bcd93c39bd4dab4ec0098367
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.4
|
Release files / phxsocket-0.1.5-py3-none-any.whl
| Download URL | phxsocket-0.1.5-py3-none-any.whl |
|---|---|
| Size | 18.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
454f448bb800e2cbbf152751121b122c101c52cd6896aa3a793c229973afefbf
|
|
BLAKE2b-256 checksum How to use checksums |
3e0c0b6ee2cfdd17987d0737cce65b16e402bb6c6424ca0c6f26e468b63af78a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.4
|
Security for phxsocket 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.