Hey there,
I’ve setup the connection of phxsocket client with pheonix server and its working very smoothly. I can send message to the server channel. But I can’t maintain its continous connection with server.
Here’s my client phxsocket code:
import phxsocket
import ssl
# Create socket client
headers = {'Authorization': 'Basic YWRtaW46YWRtaW4='}
socket = phxsocket.Client("ws://localhost:4000/socket/websocket", {"data": "STRING 1"})
# Connect and join a channel
if socket.connect(): # blocking, raises exception on failure
channel = socket.channel("room:lobby")
print("SUCCESSFULLY CONNECTED")
resp = channel.join() # also blocking, raises exception on failure
# Alternatively
def connect_to_channel(socket, origin = 'com.universal-devices.websockets.isy', subprotocols = ['ISYSUB'],extra_headers=headers):
channel = socket.channel("room:lobby")
resp = channel.join()
socket.on_open = connect_to_channel
# 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)
message = ""
# # Push data and wait for a response
message = channel.push("message:add", {"message": "HELLO WORLD"})
# Leave a channel
channel.leave()
# Disconnect
socket.close()
Seeking Guidance!