[rabbitmq-discuss] RabbitMQ with Socket Server
Chuck Remes
cremes.devlist at mac.com
Wed Sep 16 03:27:49 BST 2009
On Sep 15, 2009, at 7:19 PM, Alex Gentle wrote:
> iPhone app -> socket server -> RabbitMQ server
Great, this is very helpful.
> iPhone app has some social networking features in addition to the
> chat component. Think of it like mini-facebook inside iPhone. When a
> user types a message inside chat window of iPhone app, the message
> travels to Socket server which directs the message depending on what
> is on the message header. If the message header says "this message
> is for RabbitMQ", socket server will route to RabbitMQ. When a
> message comes from RabbitMQ to socket server, it should have message
> header that says "Route this to xxxxxx user". Then, socket server
> will route the message to appropriate user(s).
>
> It all looks good, but it doesn't work. Because I don't know how to
> make a call to RabbitMQ using the message from socket server and how
> to pass the message from RabbitMQ to socket server in binary format.
Ah, this is good.
You need to declare an exchange (of type direct, fanout or topic)
which acts as the mailbox for sending the message. Once you have
parsed the message from socket server and determined it should go to
rabbit, you publish the message to it. Here's some ruby code
(commented).
def publish_message_to_rabbit(message)
# declare the exchange
exchange = MQ.direct 'mailbox'
# send the message
exchange.publish(message, :key => 'some.routing.key')
end
Next, you need to have a client somewhere that is listening for
messages. You need to allocate a queue to receive the message and
subscribe to the routing key. Again, some ruby code.
def listen_for_rabbit_messages
# redeclare the exchange; idempotent operation
exchange = MQ.direct 'mailbox'
# declare a queue to receive the messages
queue = MQ.queue 'mailslot'
# bind the queue to the exchange and match incoming messages against
# the specified routing key
queue.bind(exchange, :key => 'some.routing.key').subscribe do |
message|
# this block is called each time a message is received
# do your message processing here
end
end
Does this help? I highly recommend you take a look through the sample
code and come back with specific questions.
cr
More information about the rabbitmq-discuss
mailing list