Skip to main content

Client library for socketcluster framework in nodejs

Project description

# socketcluster-client-python
Refer examples for more details :

Overview
--------
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
```python
sudo pip install socketclusterclient
```

Description
-----------
Create instance of `Socket` class by passing url of socketcluster-server end-point

```python
//Create a socket instance
socket = Socketcluster.socket("ws://localhost:8000/socketcluster/")

```
**Important Note** : Default url to socketcluster end-point is always *ws://somedomainname.com/socketcluster/*.

#### Registering basic listeners

Different functions are given as an argument to register listeners

```python
from socketclusterclient import Socketcluster
import logging

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)


def onconnect(socket):
logging.info("on connect got called")


def ondisconnect(socket):
logging.info("on disconnect got called")


def onConnectError(socket, error):
logging.info("On connect error got called")


def onSetAuthentication(socket, token):
logging.info("Token received " + token)
socket.setAuthtoken(token)


def onAuthentication(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()
```

#### Connecting to server

- For connecting to server:

```python
//This will send websocket handshake request to socketcluster-server
socket.connect();
```


- By default reconnection to server is disabled (since latest release) , to enable it and configure delay for connection

```python
//This will set automatic-reconnection to server with delay of 2 seconds and repeating it for infinitely
socket.setdelay(2)
socket.setreconnection(True)
socket.connect();
```

- By default logging of messages in disabled (since latest release), to enable it

```python
socket.enablelogger(True)
```

Emitting and listening to events
--------------------------------
#### Event emitter

- eventname is name of event and message can be String, boolean, int or JSON-object

```python

socket.emit(eventname,message);

# socket.emit("chat", "Hi")
```

- To send event with acknowledgement

```python

socket.emitack("chat", "Hi", ack)

def ack(eventname, error, object):
print "Got ack data " + object + " and error " + error + " and eventname is " + eventname
```

#### Event Listener

- For listening to events :

The object received can be String, Boolean, Long or JSONObject.

```python
# Receiver code without sending acknowledgement back
socket.on("ping", message)

def message(eventname, object):
print "Got data " + object + " from eventname " + eventname
```

- To send acknowledgement back to server

```python
# Receiver code with ack
socket.onack("ping", messsageack)

def messsageack(eventname, object, ackmessage):
print "Got data " + object + " from eventname " + eventname
ackmessage("this is error", "this is data")

```

Implementing Pub-Sub via channels
---------------------------------

#### Creating channel

- For creating and subscribing to channels:

```python

# without acknowledgement
socket.subscribe('yell')

#with acknowledgement
socket.subscribeack('yell', suback)

def suback(channel, error, object):
if error is '':
print "Subscribed successfully to channel " + channel
```

- For getting list of created channels :

```python
channels = socket.getsubscribedchannels()

```





#### Publishing event on channel

- For publishing event :

```python

# without acknowledgement
socket.publish('yell', 'Hi dudies')

#with acknowledgement
socket.publishack('yell', 'Hi dudies', puback)

def puback(channel, error, object):
if error is '':
print "Publish sent successfully to channel " + channel
```

#### Listening to channel

- For listening to channel event :

```python

socket.onchannel('yell', channelmessage)

def channelmessage(key, object):
print "Got data " + object + " from key " + key

```

#### Un-subscribing to channel

```python
# without acknowledgement
socket.unsubscribe('yell')

# with acknowledgement
socket.unsubscribeack('yell', unsuback)

def unsuback(channel, error, object):
if error is '':
print "Unsubscribed to channel " + channel
```

#### Disable SSL Certificate Verification

```python
socket = Socketcluster.socket("wss://localhost:8000/socketcluster/")
socket.connect(sslopt={"cert_reqs": ssl.CERT_NONE})
```

#### HTTP proxy

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".

```python
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.
```python
sclogger = socket.getlogger()
```
Please follow logging tutorial over here : https://docs.python.org/3/howto/logging-cookbook.html

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

socketclusterclient-1.3.3.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file socketclusterclient-1.3.3.tar.gz.

File metadata

File hashes

Hashes for socketclusterclient-1.3.3.tar.gz
Algorithm Hash digest
SHA256 61f8d4da0e88b1c19e9266626db9d45d42cba336ab2731cdd4e1bb584837bff2
MD5 5b1bf13e8149ad43774d619dfdbf4134
BLAKE2b-256 912d770766ea70233cce44af18ae80ec6030ff2428c5fb640686a23ca4ac99d2

See more details on using hashes here.

Supported by

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