ZeroMQ-Octave
March 29, 2024About 2 min
This section will use ZeroMQ in Octave for simple message passing, including examples of Request/Reply and Publish/Subscribe patterns.
Installing ZeroMQ
If Octave is installed on a Windows system, the zeromq package should generally be installed as well. You can confirm this by using the following command in Octave:
>> pkg list
Check the list to see if zeromq is included. If not, consider reinstalling Octave.
For Ubuntu users, you can follow these steps to install:
- Install
libzmq-dev
apt install libzmq-dev
- Install the ZeroMQ package in Octave:
pkg install -forge zeromq
Using ZeroMQ
Before using ZeroMQ in Octave, you need to load it first:
pkg load zeromq
You can use the help zeromq
command to see available functions and usage instructions.
Basic Request/Reply Pattern
Server-Side Code
pkg load zeromq % Load the zeromq library
more off % Turn off paging of output
printf("Creating hello world server...\n");
sock = zmq_socket(ZMQ_REP); % Create Reply port
zmq_bind(sock, "tcp://*:5555"); % Listen on port 5555 on all network interfaces
printf("Waiting for clients ...\n");
while (true)
recievedata = zmq_recv(sock, 10); % Receive messages of up to ten bytes
printf("Received %s\n", recievedata); % Print the received message
zmq_send(sock, "World", 5); % Respond with World
endwhile
zmq_close(sock);
Client-Side Code
pkg load zeromq % Load the zeromq library
more off % Turn off paging of output
printf("Connecting to hello world server...\n");
sock = zmq_socket(ZMQ_REQ); % Create Request port
zmq_connect(sock, "tcp://localhost:5555"); % Connect to port 5555 on localhost
for request_nbr = 1:10
printf("Sending Hello %d...\n", request_nbr);
zmq_send(sock, uint8("Hello"), 5); % Send Hello
printf("Waiting for server response %d... (Ctrl-C to exit)\n", request_nbr);
recieved = zmq_recv(sock, 10); % Receive messages of up to ten bytes
printf("Received %d = %s\n", request_nbr, recieved);
endfor
zmq_close(sock);
Basic Publish/Subscribe Pattern
Server-Side Code
pkg load zeromq % Load the zeromq library
more off % Turn off paging of output
publisher = zmq_socket(ZMQ_PUB); % Create Publish port
zmq_bind(publisher, "tcp://*:5556"); % Listen on port 5556 on localhost
while (true)
dice_number = randi(6); % Select a random integer between 1 and 6
data = sprintf("Dice %d", dice_number); % Prepare data to send
zmq_send(publisher, data); % Publish the dice number message
data = sprintf("Score %d", randi(100)); % Prepare data to send
zmq_send(publisher, data); % Publish the score message
endwhile
zmq_close(publisher);
Client-Side Code
pkg load zeromq % Load the zeromq library
more off % Turn off paging of output
subscriber = zmq_socket(ZMQ_SUB); % Create Subscribe port
zmq_connect(subscriber, "tcp://localhost:5556"); % Connect to port 5556 on localhost
msgfilter = "Dice"; % Message prefix filter
zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, msgfilter); % Set the filter
total = 0;
printf("Receiving 100 dice numbers...\n");
for update_nbr = 1:100
string = char(zmq_recv(subscriber, 128)); % Convert received message to string
[prefix, number] = sscanf (string, "%s %d", "C"); % Read the prefix string and number using C format
printf(" %d", number); % Print the number
total += number; % Calculate the total
endfor
printf("\n\nAverage = %.2f\n", total/100.0); % Print the average dice number
%fprintf("Average temperature for zipcode '%s' was %dF\n", zipfilter, (total_temp / update_nbr));
zmq_close(subscriber);
The above demonstrates how to perform basic message passing using ZeroMQ in Octave.
Exercise 1
- Implement the sample programs in Octave.
- In the second example, publish several different types of messages and try subscribing to different types of messages in the client, then process the received messages.
- Modify the connection to allow communication between different computers.