Note that there are some explanatory texts on larger screens.

plurals
  1. POAMQP creating subscribing to queues dynamically
    primarykey
    data
    text
    <p>I am trying to build a simple chat application using AMQP, Websockets and Ruby. I understand that this may not be the best use-case to understand AMQP but I would like to understand where i am going wrong. </p> <p>The following is my amqp-server code</p> <pre><code>require 'rubygems' require 'amqp' require 'mongo' require 'em-websocket' require 'json' class MessageParser # message format =&gt; "room:harry_potter, nickname:siddharth, room:members" def self.parse(message) parsed_message = JSON.parse(message) response = {} if parsed_message['status'] == 'status' response[:status] = 'STATUS' response[:username] = parsed_message['username'] response[:roomname] = parsed_message['roomname'] elsif parsed_message['status'] == 'message' response[:status] = 'MESSAGE' response[:message] = parsed_message['message'] response[:roomname] = parsed_message['roomname'].split().join('_') end response end end class MongoManager def self.establish_connection(database) @db ||= Mongo::Connection.new('localhost', 27017).db(database) @db.collection('rooms') @db end end @sockets = [] EventMachine.run do connection = AMQP.connect(:host =&gt; '127.0.0.1') channel = AMQP::Channel.new(connection) puts "Connected to AMQP broker. #{AMQP::VERSION} " mongo = MongoManager.establish_connection("trackertalk_development") EventMachine::WebSocket.start(:host =&gt; '127.0.0.1', :port =&gt; 8080) do |ws| socket_detail = {:socket =&gt; ws} ws.onopen do @sockets &lt;&lt; socket_detail end ws.onmessage do |message| status = MessageParser.parse(message) exchange = channel.fanout(status[:roomname].split().join('_')) if status[:status] == 'STATUS' queue = channel.queue(status[:username], :durable =&gt; true) unless queue.subscribed? puts "--------- SUBSCRIBED --------------" queue.bind(exchange).subscribe do |payload| puts "PAYLOAD : #{payload}" ws.send(payload) end else puts "----ALREADY SUBSCRIBED" end # only after 0.8.0rc14 #queue = channel.queue(status[:username], :durable =&gt; true) #AMQP::Consumer.new(channel, queue) elsif status[:status] == 'MESSAGE' puts "********************* Message- published ******************************" exchange.publish(status[:message) end end ws.onclose do @sockets.delete ws end end end </code></pre> <p>I use the status to indicate whether the incoming message is a message for ongoing chat or for a status message requiring me to handle chores like subscribing to the queue. </p> <p>The problem i face is that when I send a message like <code>socket.send(JSON.stringify({status:'message', message:'test', roomname:'Harry Potter'}))</code></p> <p>The <code>exchange.publish' is called but it still doesn't get pushed via the</code>ws.send` to the browser.</p> <p>Is there something fundamentally wrong with my understanding of EventMachine and AMQP? </p> <p>Here is the pastie for the same code <a href="http://pastie.org/private/xosgb8tw1w5vuroa4w7a" rel="nofollow">http://pastie.org/private/xosgb8tw1w5vuroa4w7a</a></p> <p>My code seems to work as desired when i remove the <code>durable =&gt; true</code> from <code>queue = channel.queue(status[:username], :durable =&gt; true)</code></p> <p>The following is a snippet of my Rails view which identifies the user's username and the roomname and sends it as part of message via Websockets.</p> <p>Though the code seems to work when i remove the <code>durable =&gt; true</code> I fail to understand why that affects the message being delivered. Please Ignore the mongo part of as it does not play any part yet.</p> <p>I would also like to know if my approach to AMQP and its usage is correct</p> <pre><code>&lt;script&gt; $(document).ready(function(){ var username = '&lt;%= @user.email %&gt;'; var roomname = 'Bazingaa'; socket = new WebSocket('ws://127.0.0.1:8080/'); socket.onopen = function(msg){ console.log('connected'); socket.send(JSON.stringify({status:'status', username:username, roomname:roomname})); } socket.onmessage = function(msg){ $('#chat-log').append(msg.data); } }); &lt;/script&gt; &lt;div class='block'&gt; &lt;div class='content'&gt; &lt;h2 class='title'&gt;&lt;%= @room.name %&gt;&lt;/h2&gt; &lt;div class='inner'&gt; &lt;div id="chat-log"&gt; &lt;/div&gt; &lt;div id="chat-console"&gt; &lt;textarea rows="5" cols="40"&gt;&lt;/textarea&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;style&gt; #chat-log{ color:#000; font-weight:bold; margin-top:1em; width:900px; overflow:auto; height:300px; } #chat-console{ bottom:10px; } textarea{ width:100%; height:60px; } &lt;/style&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload