matlabzeromqjeromq

JeroMQ subscriber connection breaks when recv-ing in Matlab


Using JeroMQ in Matlab, my subscriber connection drops when I try to recv a message. I have verified this with Wireshark. I also note that the closing of the TCP connection is initiated by my subscribing port, not the publishing port.

javaclasspath('jeromq-0.5.1.jar')
import org.zeromq.*;

ctx = zmq.Ctx();
socket = ctx.createSocket(ZMQ.SUB);
socket.connect('tcp://127.0.0.1:5996')
message = socket.recv(1) %this is when the connection gets dropped

Wireshark screenshot showing connection being closed

I don't know for sure if it would help, but I have investigated using this,

socket.setSocketOpt(ZMQ.ZMQ_TCP_KEEPALIVE,1)

but Matlab reports that ZMQ_TCP_KEEPALIVE is an unrecognized function or variable.

I am indebted to the folks on this thread for getting me started on the right foot with JeroMQ in Matlab.


Solution

  • I did some more research and factored in suggestions from some friends over at Github. Turns out that my socket wasn't closing until later in the code at my close() function. Here is my working code for others looking to get started using JeroMQ in Matlab. Seems that some of the code on other posts is antiquated.

    javaclasspath('jeromq-0.5.1.jar')
    import org.zeromq.*;
    
    %subscribe to ZMQ feed
    context = ZContext();
    socket = context.createSocket(ZMQ.SUB); 
    success = false;
    while(~success)
        success = socket.connect('tcp://127.0.0.1:5996');
    end
    socket.subscribe("");
    socket.setTCPKeepAlive(1);
    
    %receive a message
    message = socket.recv(0); %nonblocking receive uses argument (1)
    
    %when done
    socket.close();