Skip to main content

Websocket client for Phoenix Elixir

Project description

phxsocket

Synchronous phoenix websocket client using callbacks

Phoenix channels

Requirements

websockets

Usage

Import the package

import phxsocket

Create socket client

socket = phxsocket.Client("wss://target.url/channel/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"})
  resp = channel.join() # also blocking, raises exception on failure

Alternatively

def connect_to_channel(socket):
  channel = socket.channel("room:roomname", {"more options": "something else"})
  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.3.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

phxsocket-0.1.3-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file phxsocket-0.1.3.tar.gz.

File metadata

  • Download URL: phxsocket-0.1.3.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for phxsocket-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f41fa39fef7f203dee181f64079f3b40f15b7ac1c136595802d706852840f08c
MD5 9d9943f0a62c386eaec4b7c66b2ba2fc
BLAKE2b-256 9d18b1c10f99fcc15573f025ec993381cd9be9efdf69ed25dbeac6f373a67885

See more details on using hashes here.

File details

Details for the file phxsocket-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: phxsocket-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for phxsocket-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 82178bda22f0b8887a52de1a553fbff7518987663ae13f46e9d1c8b6111ad222
MD5 6938622e915bcd5ff34d7d28fc87608d
BLAKE2b-256 7a16efaf192ec21638ad9d2e9279aaf28cb820cb4c8d99ebd3466ff7294ad7ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page