Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created January 19, 2011 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/786508 to your computer and use it in GitHub Desktop.
Save anonymous/786508 to your computer and use it in GitHub Desktop.
php publisher
<?php
// Create a connection
$cnn = new AMQPConnection();
$cnn->connect();
// Declare a new exchange
$ex = new AMQPExchange($cnn);
$ex->declare('default');//, AMQP_EX_TYPE_DIRECT);
// Create a new queue
$q = new AMQPQueue($cnn);
$q->declare('queue1');
// Bind it on the exchange to routing.key
$ex->bind('queue1', 'routing.key');
// Publish a message to the exchange with a routing key
$ex->publish('message', 'routing.key');
// Read from the queue
$msg = $q->consume();
?>
$:.unshift File.dirname(__FILE__) + '/../../lib'
require 'mq'
require 'pp'
EM.run do
# connect to the amqp server
connection = AMQP.connect(:host => 'localhost', :logging => false)
# open a channel on the AMQP connection
channel = MQ.new(connection)
# declare a queue on the channel
queue = MQ::Queue.new(channel, 'queue1')
# create a fanout exchange
exchange = MQ::Exchange.new(channel, :direct, 'direct1')
# bind the queue to the exchange
queue.bind(exchange)
# subscribe to messages in the queue
queue.subscribe do |headers, msg|
pp [:got, headers, msg]
connection.close{ EM.stop_event_loop }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment