Websocket client for Phoenix Elixir
Project description
phxsocket
Synchronous phoenix websocket client using callbacks
Requirements
websockets
Usage
Import the package
import phxsocket
Create socket client
socket = phxsocket.Client("wss://target.url/websocket", {"options": "something"})
Connect and join a channel
if socket.connect(): # blocking, raises exception on failure
channel = socket.channel("room:roomname", {"more options": "something else"})
join_success, resp = channel.join()
Alternatively
def connect_to_channel(socket):
channel = socket.channel("room:roomname", {"more options": "something else"})
join_success, 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):
thing = payload["thing"]
channel.on("eventname", do_something)
Push data to a channel
channel.push("eventname", {"some": "data"})
Push data and wait for a response
message = channel.push("eventname", {"some": "data"}, reply=True)
response = message.wait_for_response() # blocking
Push data and react to the response with a callback
def respond(payload):
print(payload)
channel.push("eventname", {"some": "data"}, respond)
Leave a channel
channel.leave()
Disconnect
socket.close()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
phxsocket-0.1.1.tar.gz
(4.4 kB
view hashes)
Built Distribution
phxsocket-0.1.1-py3-none-any.whl
(17.1 kB
view hashes)
Close
Hashes for phxsocket-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c6500d1f4a9a1722473fb64c462dc96c82b81db24a97b75ce79902093813ab2 |
|
MD5 | 900fbb37710a0ce099a361cae869eff4 |
|
BLAKE2b-256 | 79070d52d21e3e47471bd69045e7186c5bb738fcebdb8561dc39b7cedced50f8 |