Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to store the 'channel' from the raw communication in a python variable? #15

Closed
wilsoff opened this issue Mar 20, 2018 · 1 comment
Closed

Comments

@wilsoff
Copy link

wilsoff commented Mar 20, 2018

Thanks so much for this library! I've got it streaming the information (for quoinex.com) to the screen using one of your examples, via both logging and print.

However, I'm finding that logging provides more information than print at the moment - logging includes event and channel. Is there a way to access these bits of information outside of logging?

The problem I have is that if I subscribe to lots of channels, I'm not sure how I'll identify which response belongs to which channel. An ugly workaround below is to create a callback function for each subscription, but I assume there is a far more elegant way.

Any help would be appreciated please!

import pysher
import sys
import time

# Add a logging handler so we can see the raw communication data
import logging
root = logging.getLogger()
root.setLevel(logging.INFO)
ch = logging.StreamHandler(sys.stdout)
root.addHandler(ch)

pusher = pysher.Pusher('XXXX') #key removed

def channel_callbacksell(message):
    response = "btcjpy_sell: %s" % message
    print(response)
    
def channel_callbackbuy(message):
    response = "btcjpy_buy: %s" % message
    print(response)
    
def connect_handler(data):
    channelsell = pusher.subscribe('price_ladders_cash_btcjpy_sell')
    channelsell.bind('updated', channel_callbacksell)
    channelbuy = pusher.subscribe('price_ladders_cash_btcjpy_buy')
    channelbuy.bind('updated', channel_callbackbuy)
        
pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()

while True:
    time.sleep(1)

The results I get to screen are:

Logging Output (with 'event' and 'channel'):

{"event":"updated","channel":"price_ladders_cash_btcjpy_buy","data":"[[\"902803.86848\",\"0.53500000\"],[\"902525.42000\",\"0.24500000\"],[\"902500.00000\",\"0.35320000\"],[\"902041.67444\",\"0.23510000\"],[\"901864.00000\",\"0.01856800\"],[\"901525.40000\",\"0.02000000\"],[\"901518.40000\",\"0.08000000\"],[\"900968.40000\",\"0.02000000\"],[\"900967.63000\",\"0.01609600\"],[\"900936.40000\",\"0.02000000\"],[\"900830.39000\",\"0.02000000\"],[\"900620.00000\",\"2.00000000\"],[\"900532.41000\",\"0.01000000\"],[\"899420.00000\",\"0.01000000\"],[\"899394.25718\",\"0.09366000\"],[\"899379.78211\",\"0.09459000\"],[\"899344.29123\",\"0.18108000\"],[\"899326.67050\",\"0.10873000\"],[\"899244.96171\",\"0.42860000\"],[\"899209.18772\",\"0.42569000\"],[\"899194.03279\",\"0.43184000\"],[\"899182.76156\",\"0.18316000\"],[\"899111.77941\",\"0.42505000\"],[\"899065.00000\",\"1.10000026\"],[\"899064.65503\",\"0.83353000\"],[\"899024.92879\",\"0.74487000\"],[\"899000.00000\",\"0.07000000\"],[\"898784.00000\",\"2.37200000\"],[\"898774.00000\",\"0.10000000\"],[\"898733.00000\",\"2.36500000\"],[\"898710.00000\",\"0.02000000\"],[\"898593.50106\",\"0.87417000\"],[\"898517.91761\",\"0.99008000\"],[\"898504.00000\",\"0.00900000\"],[\"898467.31863\",\"3.68813000\"],[\"898121.09000\",\"3.00000000\"],[\"898000.00000\",\"0.07000000\"],[\"897457.94012\",\"4.23122000\"],[\"897337.00000\",\"2.37200000\"],[\"897000.00000\",\"0.07000000\"]]"}

Print Output (with just 'data'):

[["902803.86848","0.53500000"],["902525.42000","0.24500000"],["902500.00000","0.35320000"],["902041.67444","0.23510000"],["901864.00000","0.01856800"],["901525.40000","0.02000000"],["901518.40000","0.08000000"],["900968.40000","0.02000000"],["900967.63000","0.01609600"],["900936.40000","0.02000000"],["900830.39000","0.02000000"],["900620.00000","2.00000000"],["900532.41000","0.01000000"],["899420.00000","0.01000000"],["899394.25718","0.09366000"],["899379.78211","0.09459000"],["899344.29123","0.18108000"],["899326.67050","0.10873000"],["899244.96171","0.42860000"],["899209.18772","0.42569000"],["899194.03279","0.43184000"],["899182.76156","0.18316000"],["899111.77941","0.42505000"],["899065.00000","1.10000026"],["899064.65503","0.83353000"],["899024.92879","0.74487000"],["899000.00000","0.07000000"],["898784.00000","2.37200000"],["898774.00000","0.10000000"],["898733.00000","2.36500000"],["898710.00000","0.02000000"],["898593.50106","0.87417000"],["898517.91761","0.99008000"],["898504.00000","0.00900000"],["898467.31863","3.68813000"],["898121.09000","3.00000000"],["898000.00000","0.07000000"],["897457.94012","4.23122000"],["897337.00000","2.37200000"],["897000.00000","0.07000000"]]
@wilsoff
Copy link
Author

wilsoff commented Mar 21, 2018

Got this working by taking another look at your example. Solved it by passing the following keyword: channel=channel.__dict__['name']

import sys
import time
import pysher

# Add a logging handler so we can see the raw communication data
import logging
root = logging.getLogger()
root.setLevel(logging.INFO)
ch = logging.StreamHandler(sys.stdout)
root.addHandler(ch)

pusher = pysher.Pusher(appkey)

def  my_func(*args, **kwargs):
    print("processing Args:", args)
    print("processing Kwargs:", kwargs)

def connect_handler(data):
    channel = pusher.subscribe('mychannel')
    channel.bind('myevent', my_func, channel=channel.__dict__['name'], event='updated')

pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()

while True:
    time.sleep(1)

@wilsoff wilsoff closed this as completed Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant