pyzmq icon indicating copy to clipboard operation
pyzmq copied to clipboard

how to work with websocket

Open wwj718 opened this issue 4 years ago • 9 comments

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

wwj718 avatar Mar 19 '20 05:03 wwj718

I'm having the same problem. Any updates on this?

luigifcruz avatar Apr 01 '20 23:04 luigifcruz

Any updates on this? Looking for a reversable http proxy solution for zmq servers.

hu6360567 avatar Apr 22 '20 02:04 hu6360567

I am having the same problem. What's the best way to communicate between the python server and the web client?

mzy2240 avatar Apr 22 '20 17:04 mzy2240

@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.)

sakurai-youhei avatar May 15 '20 09:05 sakurai-youhei

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 :)

jfrey-xx avatar May 17 '20 02:05 jfrey-xx

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?

mzy2240 avatar May 17 '20 04:05 mzy2240

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:)

jfrey-xx avatar May 17 '20 11:05 jfrey-xx

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.

dengf avatar Jun 26 '20 11:06 dengf

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?

stcleezy avatar Jan 22 '21 22:01 stcleezy

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)

Elsaligi avatar Dec 07 '22 06:12 Elsaligi

The examples have a complete example of installing libzmq with drafts and then linking libzmq to it.

minrk avatar Dec 07 '22 09:12 minrk

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... image

Elsaligi avatar Dec 07 '22 13:12 Elsaligi

pyzmq 26 has updated libzmq builds and should be a little easier to build both pyzmq and libzmq with drafts reliably.

minrk avatar Feb 27 '24 15:02 minrk