Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (46 sloc) 2.72 KB


EM-PosixMQ integrates posix_mq Ruby library into EventMachine, allowing asynchronous reading from a POSIX message queue.

For detailed information about the usage of POSIX message queues check the manpages of POSIX message queues and documentation of the Ruby bindings.

Usage Example

Server (reads from a POSIX message queue):

require "em-posixmq"

class MyPosixMQ < EM::PosixMQ::Watcher
  def receive_message(message, priority)
    puts "received message with priority #{priority}: #{message.inspect}"
end do
  posix_mq = "/my_posix_mq", IO::CREAT | IO::RDONLY | IO::NONBLOCK posix_mq, MyPosixMQ

Client (writes into a POSIX message queue):

require "posix_mq"
require "securerandom"

posix_mq = "/my_posix_mq", IO::CREAT | IO::WRONLY | IO::NONBLOCK

while true do
  message = SecureRandom.hex 6
  priority = SecureRandom.random_number 9

  puts "sending message '#{message}' with priority #{priority} to the queue..."
  posix_mq.send message, priority

  sleep SecureRandom.random_number

The client script will write random messages with random priority into a POSIX message queue named "/my_posix_mq" at variable intervals.

Creating a Class Handler

In order to use EM-PosixMQ a class handler must be created. Such class must inherit from EM::PosixMQ::Watcher and define the method receive_message which would be called with parameters message (a String) and priority (a Fixnum) upon receipt of a message from the message queue.

class MyPosixMQ < EM::PosixMQ::Watcher
  def receive_message(message, priority)
    # do something with the message (and priority).

Runnig the Server posix_mq, Handler

Attaches a POSIX_MQ instance to the EventMachine reactor for asynchronous reading. The method requires two parameters:

  • posix_mq - Instance of POSIX_MQ class being managed by EventMachine. The instance must be opened with read access.
  • Handler - The name of the class inheriting from EM::PosixMQ::Watcher and defining the method receive_message.


EM-PosixMQ is provided as a Ruby Gem:

~$ gem install em-posixmq

Supported Platforms

EM-PosixMQ depends on POSIX message queues which are implemented in Linux and BSD.


Many thanks to Eric Wong, the author of posix_mq Ruby library.

Jump to Line
Something went wrong with that request. Please try again.