pyzmq
pyzmq copied to clipboard
how to work with websocket
Hi, I am working with libzmq DRAFT sockets
>>> print('pyzmq-%s' % zmq.__version__)
pyzmq-19.0.0
>>> print('libzmq-%s' % zmq.zmq_version())
libzmq-4.3.2
>>> print('Draft API available: %s' % zmq.DRAFT_API)
Draft API available: True
but when I run the code:
# python rep.py
import zmq
import time
import sys
port = "8001"
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("ws://*:%s" % port)
while True:
# Wait for next request from client
message = socket.recv()
print("Received request: ", message)
time.sleep (1)
socket.send("World from %s" % port)
I encountered an error
zmq.error.ZMQError: Protocol not supported
What should I do?
thanks!
https://github.com/zeromq/jszmq works well
I'm having the same problem. Any updates on this?
Any updates on this? Looking for a reversable http proxy solution for zmq servers.
I am having the same problem. What's the best way to communicate between the python server and the web client?
@wwj718 and @mzy2240, Why are you trying ZeroMQ through pyzmq just for ws://
? If you aim neither 39/ZWS (which ZWSSock implements) nor 45/ZWS (which jszmq implements), then e.g. Websocket Server fits your purpose like below code.
from websocket_server import WebsocketServer
def message_received(client, server, message):
print("Received:", message)
server.send_message(client, "Reply to '{}'".format(message))
with WebsocketServer(port=8001) as server:
server.set_fn_message_received(message_received)
server.run_forever()
FYI: zmq_bind() accepts only tcp://
, ipc://
, inproc://
, pgm://
, epgm://
and vmci://
as of ZeroMQ 4.3. (udp://
seems to be introduced in the future, though.)
Hello,
I managed to make websocket work, you actually have to compile libzmq from the master branch, not from release, as mentioned in https://github.com/zeromq/jszmq/issues/9 . Once done, I could to use ws://
transport between two python programs, as well as between python and jszmq :)
Hello,
I managed to make websocket work, you actually have to compile libzmq from the master branch, not from release, as mentioned in zeromq/jszmq#9 . Once done, I could to use
ws://
transport between two python programs, as well as between python and jszmq :)
Wow that's awesome! Just to clarify, you mean to compile the pyzmq from scratch using the master branch, right? BTW, is it possible to compile from scratch for Windows?
I did not even have to build pyzmq, just fetch it from pip, setting prefix to point to compiled libzmq and enabling the draft option, as mentionned in the link of the first post. No idea about Windows, tested with Ubuntu 16.04 and python 2.7 (:older_man:)
I did not even have to build pyzmq, just fetch it from pip, setting prefix to point to compiled libzmq and enabling the draft option, as mentionned in the link of the first post. No idea about Windows, tested with Ubuntu 16.04 and python 2.7 (👴)
Hi, Jfrey-xx, thanks for the solution here. I followed your instructions and am able to get ws works as well on Linux red hat.
Websocket transport for REQ/REP appears to work fine, but PUB/SUB isn't working for me. This appears to be supported in libzmq, and can be exercised successfully with the jszmq library
Here is my simple test (see inline comments below):
#sub.py
context = zmq.Context()
socket = context.socket(zmq.SUB)
# socket.bind("ipc:///tmp/test.pipe") # this works
# socket.bind("tcp://*:8003") # this works
socket.bind("ws://*:8003") # THIS DOES NOT WORK
socket.setsockopt_string(zmq.SUBSCRIBE, '')
while True:
string = socket.recv_string()
print(string)
#pub.py
context = zmq.Context()
socket = context.socket(zmq.PUB)
# socket.connect("ipc:///tmp/test.pipe") # this works
# socket.connect("tcp://127.0.0.1:8003") # this works
socket.connect("ws://127.0.0.1:8003") # THIS DOES NOT WORK
while True:
socket.send_string("Test")
time.sleep(1)
Are others seeing the same behavior?
I did not even have to build pyzmq, just fetch it from pip, setting prefix to point to compiled libzmq and enabling the draft option, as mentionned in the link of the first post. No idea about Windows, tested with Ubuntu 16.04 and python 2.7 (👴)
How to point to compiled libzmq and enable drafts. Any help is appreciated..Thanks in advance. I am working on raspberrypi(32bit)
The examples have a complete example of installing libzmq with drafts and then linking libzmq to it.
The examples have a complete example of installing libzmq with drafts and then linking libzmq to it.
Great.. thank you so much for the help.You saved my day.. But even after DRAFT_API=False even DRAFT API enabled..
It is not enablign actually...
pyzmq 26 has updated libzmq builds and should be a little easier to build both pyzmq and libzmq with drafts reliably.