node-svmq
Native System V IPC message queues in Node.js with bindings and an easy to use abstraction. System V message queues are more widely implemented on UNIX systems than POSIX message queues, and are supported on both Linux and OS X.
These are FIFO queues. New messages are pushed to the end and old messages are popped off first.
Why?
The keyword here is IPC. These queues exist in kernel space; they can be accessed by multiple processes, assuming the correct permissions, etc. This provides a portable method for passing messages between two completely unrelated processes using potentially different languages/runtimes.
For example, PHP has native support for SysV IPC including message queues. That means you can now easily serialize PHP objects to JSON and pass them to Node.js (and vice versa) without messing with pipes, sockets, etc. and without having to spawn processes from within Node.js.
Installation
npm install svmq
Native bindings are written in C/C++ using NAN and built automatically on install.
Usage
Creating or opening a message queue
// Opens or creates the queue specified by key 31337var queue = ;// ORvar MessageQueue = ;var queue = 31337;
Listen for new messages
// Listen for new messages on the queue// If the queue already contains messages, they will be popped off first (one at a time).queue;
Push message onto queue
// Push a new message to the queuequeue; // Note that SysV message queues may block under certain circumstances, so you cannot assume that// the above message will already be in the queue at this point.// Use the callback to know exactly when the message has been pushed to the queue.
Pop message off queue
// Pop a message off of the queue// Do not use pop() with the 'data' event; use one or the other.queue;
Close or dispose of a queue
// Close the queue immediately// Returns true/false, specifying whether or not the queue closed.// Can be used with a callback to catch errors on close.//// Note: this may require escalated privileges on some OSes.var closed = queue;// OR (closed status will be returned and passed to callback)var closed = queue;
Multiplexing / Two-way communication
Opening a queue via the same key from two different processes will give both processes access to the same data structure, but after all, it's still a queue. This means that proper two-way communication needs either two queues or some way to multiplex one queue.
Fortunately, each message sent to the queue has an associated message type.
When you pop a message off, you ask for message type 0
by default. This tells
the system that you want the message from the front of the queue, no matter what
message type it is. If you pass a positive integer greater than zero, the queue
will give you the first message that resides in the queue with that message type
or block until it's available (via callback).
In other words, you can achieve two-way communication between processes by using two distinct message types. See the example below.
// Process Avar queue = MessageQueue;queue;queue; // Process Bvar queue = MessageQueue;queue;queue;
Note: the 'data' event does not support message types yet, so you'll have to construct your own receiving loop. See the example below.
var queue = MessageQueue; { queue;} ;
Access to native bindings
Using these is not recommended, but you're more than welcome to mess around with them.
// Simplified JS bindings to the C syscalls.// Blocking calls use a callback as the last parameter.//// msgget, msgsnd, msgrcv, msgctl// See: http://linux.die.net/man/7/svipcvar svmq = ;var msg = svmqmsg;var MSGMAX = svmqMSGMAX; // max message data size (hardcoded) // Open/create a queue with key 31337 and flags 950 (0666 | IPC_CREAT)// Throws an error on failurevar id = msg; // Push a string to the queuemsg; // Pop message off queue with max buffer size MSGMAXvar bufferSize = MSGMAX;msg; // Close/delete a queue// Throws an error on failuremsg;