Python Websocket Speed


Good Day,

I have tried to retrieve data from a web-socket using authobahn’s python library but it doesn’t seem to be retrieving them fast enough. I have attached the following code which i have tried but it seem to only be receiving data every 25ms or (40 data/sec) whereas the web-socket server is said to be sending data out every 4ms or (250 data/sec). Am i reaching some bottle neck or am i going about this the wrong way?

Also, i’m not too familiar with web sockets as this is the first time i’m trying to use them so i’m sorry if this is a simple question. If it helps, i’m trying to receive the data points and plot them in python after some processing but i’m still at the stage where i need to be receiving the data much faster.

I would appreciate it if someone could point me in the right direction. Thanks! :slight_smile:

import time
import simplejson as json
from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory

import wsaccel

class MyClientProtocol(WebSocketClientProtocol):

    counter = 0
    start_time = time.time()

    def onConnect(self, response):
        print("Server Connected: {0}".format(response.peer))

    def onOpen(self):
        print("WebSocket Connection Open.")


    def onMessage(self, payload, isBinary):
        if isBinary == 0:
            payload = payload.decode('utf8')
            data = json.loads(payload)
            MyClientProtocol.counter += 1

            if MyClientProtocol.counter == 1000:
                print "Counter: ", MyClientProtocol.counter
                print "Time Taken: ", time.time() - MyClientProtocol.start_time
                print "Data Points/Second: ", MyClientProtocol.counter / (time.time() - MyClientProtocol.start_time)

    def onClose(self, wasClean, code, reason):
        print("WebSocket Connection Closed: {0}".format(reason))

if __name__ == '__main__':

    import sys
    from twisted.python import log
    from twisted.internet import reactor


    factory = WebSocketClientFactory("ws://", debug=False)
    factory.protocol = MyClientProtocol

    reactor.connectTCP("", 9001, factory)