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.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: phxsocket-0.1.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.10

File hashes

Hashes for phxsocket-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9d1c7295712d98dd784fa31f7b280bb9794522f89a585d90c91c2e99ae34c4ba
MD5 a3a11a605af0c4de14c1c102b941a92b
BLAKE2b-256 c57f4be9cf244b5a2340d04925cdae222285e57083e5b7432f2196bfc04df5c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: phxsocket-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.10

File hashes

Hashes for phxsocket-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8bf39542ef8eb9fd1cbcb79742dc984dae4cb6ba6a8773564912a127640abda4
MD5 651bcd0aac2cfa79cd11350b6e031184
BLAKE2b-256 6532a5b3803b96ded56140288c328f1c0f30ec2277a378c8fd7d5e3883c53f3f

See more details on using hashes here.

Supported by

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