flask-sockets and ZeroMQ
<p>I'm playing around with getting <code>zeromq</code> pub/sub messages sent up to a browser with a websocket. The following "works", in that messages do get sent up through a websocket. However, trying to reload the page just hangs, as I'm guessing the <code>while True</code> loop is blocking. I thought that the <code>gevent.sleep()</code> call would allow a context switch, but apparently not. Any idea how to get all these parts working together? </p>

<pre><code>import zmq
import json
import gevent
from flask_sockets import Sockets
from flask import Flask, render_template
import logging
from gevent import monkey

monkey.patch_all()

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

sockets = Sockets(app)
context = zmq.Context()

ZMQ_LISTENING_PORT = 6557

@app.route('/')
def index():
    return render_template('index.html')

@sockets.route('/zeromq')
def send_data(ws):
    logger.info('Got a websocket connection, sending up data from zmq')
    socket = context.socket(zmq.SUB)
    socket.connect('tcp://localhost:{PORT}'.format(PORT=ZMQ_LISTENING_PORT))
    socket.setsockopt(zmq.SUBSCRIBE, "")
    gevent.sleep()
    while True:
        data = socket.recv_json()
        logger.info(data)
        ws.send(json.dumps(data))
        gevent.sleep()

if __name__ == '__main__':
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer(('', 25000), app, handler_class=WebSocketHandler)
    server.serve_forever()
</code></pre>

<p>Nevermind, just realized need to do </p>

<pre><code>import zmq.green as zmq
</code></pre>

<p>in order to use with gevent compatibility. <a href="https://pyzmq.readthedocs.io/en/latest/api/zmq.green.html" rel="nofollow">Here's a link to pyZeormq docs.</a></p>

python zeromq flask-sockets