stdlib : zeromq

This provides a binding to the ZeroMQ, a socket library.
bind   close   connect   new   open   receive   send   set_opt   term  

Here is a simple server which will echo whatever messages it receives back to the sender:

include :zeromq

#Create a new context
z = zeromq.new

#Create a new 'reply' socket
s = z.open zeromq.REP

#Bind to localhost using TCP on port 5555
s.bind "tcp://lo:5555"

loop {

  #Receive a message
  input = s.receive

  #Send the message right back
  s.send input
}

Here is a client that will connect to the server above:

include :zeromq

#Create a new context
z = zeromq.new

#Open a new 'request' socket
s = z.open zeromq.REQ

#Connect to the local server
s.connect "tcp://localhost:5555"

while {
  #Get user input
  input = ask '?'

  #Allow 'q' to quit the client
  false? input == "q"
    {
      #Send the input to the server
      s.send input

      #Print the response
      p s.receive

      #Continue looping
      true
    }
}

#Close the socket
s.close

#Cleanup the context
z.term

bind

socket.bind address

Binds the socket to the given address to accept connections.

close

socket.close

Close the socket.

connect

socket.connect address

Connect the socket to a given address.

new

zmq.new
zmq.new threads

Create a new ZeroMQ context with the given number of threads. If no number is specified, the default is a single thread.

open

zmq.open type

This returns a new socket of the given type. type will be one of

  • zeromq.REQ
  • zeromq.REP
  • zeromq.DEALER
  • zeromq.ROUTER
  • zeromq.PUB
  • zeromq.SUB
  • zeromq.PUSH
  • zeromq.PULL
  • zeromq.PAIR

For a description of these options, see zmq_socket(3).

receive

socket.receive

Receive a message. Returns the message as a string.

send

socket.send message

Sends a string.

set_opt

socket.set_opt option, value

Set options on a socket.

term

zmq.term

Terminates the ZeroMQ context.

Fork me on GitHub