Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set up a Pusher server with Flask?

I am trying to setup a simple flask server:

import envkey
import pysher
from flask import Flask
# from predictor import PythonPredictor
app = Flask(__name__)


pusher = pysher.Pusher(envkey.get('PUSHER_KEY'))


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

# We can't subscribe until we've connected, so we use a callback handler
# to subscribe when able


def connect_handler(data):
    print('connect habndler')
    channel = pusher.subscribe('mychannel')
    channel.bind('myevent', my_func)


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


@app.route('/')
def index():

    pusher.connect()

    return 'Server Works!'

But I Get an error:

RuntimeError: cannot join current thread

What am I doing wrong?

like image 374
Shamoon Avatar asked Jun 24 '20 16:06

Shamoon


1 Answers

Specifying my Pusher cluster during initalization helped me get rid of that issue:

pusher = pysher.Pusher(
    key=envkey.get('PUSHER_KEY'),  # Or however you get the key
    cluster="eu",  # Add cluster!
)
like image 197
errata Avatar answered Sep 20 '22 15:09

errata