Refer examples for more details :
This client provides following functionality
- Easy to setup and use
- Can be used for extensive unit-testing of all server side functions
- Support for emitting and listening to remote events
- Automatic reconnection
- Pub/sub
- Authentication (JWT)
- Support for python2.x.x / python3.x.x
To install use
sudopipinstallsocketclusterclientCreate instance of Socket class by passing url of socketcluster-server end-point
//Createasocketinstancesocket=Socketcluster.socket("ws://localhost:8000/socketcluster/") Important Note : Default url to socketcluster end-point is always ws://somedomainname.com/socketcluster/.
Different functions are given as an argument to register listeners
fromsocketclusterclientimportSocketclusterimportlogginglogging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
defonconnect(socket):
logging.info("on connect got called")
defondisconnect(socket):
logging.info("on disconnect got called")
defonConnectError(socket, error):
logging.info("On connect error got called")
defonSetAuthentication(socket, token):
logging.info("Token received "+token)
socket.setAuthtoken(token)
defonAuthentication(socket, isauthenticated):
logging.info("Authenticated is "+str(isauthenticated))
if__name__=="__main__":
socket=Socketcluster.socket("ws://localhost:8000/socketcluster/")
socket.setBasicListener(onconnect, ondisconnect, onConnectError)
socket.setAuthenticationListener(onSetAuthentication, onAuthentication)
socket.connect()- For connecting to server:
//Thiswillsendwebsockethandshakerequesttosocketcluster-serversocket.connect();- By default reconnection to server is disabled (since latest release) , to enable it and configure delay for connection
//Thiswillsetautomatic-reconnectiontoserverwithdelayof2secondsandrepeatingitforinfinitelysocket.setdelay(2)
socket.setreconnection(True)
socket.connect();- By default logging of messages in disabled (since latest release), to enable it
socket.enablelogger(True)- eventname is name of event and message can be String, boolean, int or JSON-object
socket.emit(eventname,message);
# socket.emit("chat", "Hi")- To send event with acknowledgement
socket.emitack("chat", "Hi", ack) defack(eventname, error, object):
print"Got ack data "+object+" and error "+error+" and eventname is "+eventname- For listening to events :
The object received can be String, Boolean, Long or JSONObject.
# Receiver code without sending acknowledgement backsocket.on("ping", message)
defmessage(eventname, object):
print"Got data "+object+" from eventname "+eventname- To send acknowledgement back to server
# Receiver code with acksocket.onack("ping", messsageack)
defmesssageack(eventname, object, ackmessage):
print"Got data "+object+" from eventname "+eventnameackmessage("this is error", "this is data")
- For creating and subscribing to channels:
# without acknowledgementsocket.subscribe('yell')
#with acknowledgementsocket.subscribeack('yell', suback)
defsuback(channel, error, object):
iferroris'':
print"Subscribed successfully to channel "+channel- For getting list of created channels :
channels=socket.getsubscribedchannels()- For publishing event :
# without acknowledgementsocket.publish('yell', 'Hi dudies')
#with acknowledgementsocket.publishack('yell', 'Hi dudies', puback)
defpuback(channel, error, object):
iferroris'':
print"Publish sent successfully to channel "+channel- For listening to channel event :
socket.onchannel('yell', channelmessage)
defchannelmessage(key, object):
print"Got data "+object+" from key "+key# without acknowledgementsocket.unsubscribe('yell')
# with acknowledgementsocket.unsubscribeack('yell', unsuback) defunsuback(channel, error, object):
iferroris'':
print"Unsubscribed to channel "+channelsocket=Socketcluster.socket("wss://localhost:8000/socketcluster/")
socket.connect(sslopt={"cert_reqs": ssl.CERT_NONE})Support websocket access via http proxy. The proxy server must allow "CONNECT" method to websocket port. Default squid setting is "ALLOWED TO CONNECT ONLY HTTPS PORT".
socket=Socketcluster.socket("wss://localhost:8000/socketcluster/")
socket.connect(http_proxy_host="proxy_host_name", http_proxy_port=3128)- To have custom settings over internal logger, you can get logger instance and apply necessary settings over it.
sclogger=socket.getlogger()Please follow logging tutorial over here : https://docs.python.org/3/howto/logging-cookbook.html