Getting Started with Streaming data

jvshk78trade
Can somebody explain how to read streaming data from web socket object?, I'm able to instantiate web socket class, connect(threaded=True), subscribe instrument and set mode.Just didn't understand succeed in reading streaming data.
  • Vivek
    Vivek edited August 2017
    Hi,

    You can try this example - https://gist.github.com/vividvilla/10a5ca6e5a479a904e0fdb66dfd6a108

    You need to use option threaded=True incase you don't want to block your main thread and run WebSocket in different thread than your main thread. You can read about threading in python here.
  • Vivek
    @jvshk78trade Following up with my previous answer. Here is an example where I have enabled threaded option and running WebSocket in different thread but changing modes every 5 second in main thread - https://gist.github.com/vividvilla/db62e69af44831a6c4126ed193ea5214
  • jvshk78trade
    Hi @Vivek ,

    Thanks for the prompt reply, my object oriented programming skills are bit rusty, from the above suggested example can u pl further explain the following :

    kws.on_tick = on_tick
    kws.on_connect = on_connect

    as per my understanding "kws.on_tick/on_connect" is an instance variable,"on_tick" is an user defined function, what happening in "kws.on_tick = on_tick"

  • Vivek
    @jvshk78trade on_tick function is a user defined callback and when you assign it like this kws.on_tick = on_tick then on_tick function will be called whenever there is a tick received with two params tick and ws where tick is the array of ticks object and ws is the WebSocket object.

    You can read more about callbacks here - https://stackoverflow.com/questions/824234/what-is-a-callback-function
  • jvshk78trade
    jvshk78trade edited August 2017
    @Vivek So, on_tick function is invoked by kws.on_tick which also puts the needed parameters in the function, I will try out some examples.

    Thanks for the much needed guidance,
  • jvshk78trade
    @Vivek one last query, is "kws.on_tick" a method or variable , in the WebSocket documentation I'm seeing it as instance variable.
  • Vivek
    @jvshk78trade Yeah its a instance variable of instance kws.
  • jvshk78trade
    @Vivek I think I'm now understanding the complete picture:

    1) "kws.on_tick" is used as parameter in some mysterious method defined in WebSocket class.
    2)"kws.on_tick = on_tick" basically makes "kws.on_tick" a pointer to "on_tick ()" function which is going into whichever function is taking in "kws.on_tick"
  • Vivek
    @jvshk78trade Yes, you assign the callback function to instance attribute (which becomes the instance method) and we call the assigned function.
  • soumyadeep
    Hi @Vivek in the threaded example script, is it necessary to change modes every 5 seconds? I mean why would you do that?
  • Vivek
    @soumyadeep Thats an example which shows you that you can change mode or subscribe to any other instruments while websocket is running in different thread. Its just an basic example and you might be doing the stuffs related to your app logic.
Sign In or Register to comment.