2017-05-31 91 views
0

我是AMQP/RabbitMQ新手,和相關的Node.js新手。我可以使用amqplib NPM庫客戶端嗎?我可以在瀏覽器中使用amqplib嗎?

我希望能夠從我的Angular應用程序直接將消息推送到RabbitMQ。我用Browserify模塊化了很多客戶端代碼。我現在開始嘗試使用RabbitMQ,並希望通過amqp協議將消息直接從瀏覽器推送到基於雲的隊列。

我已經通過NPM安裝amqplib並寫入/粘貼以下模塊:

var amqp = require('amqplib/callback_api'); 

var push = function(){ 
    console.log('This is the CORE queue.pusher push function being triggered'); 

    var connString = 'amqp://username:[email protected]/username'; 

    amqp.connect(connString, function(err, conn) { 

     if (err){ 
      console.log("core queue.pusher push error %s", err); 
     }else { 
      conn.createChannel(function (err, ch) { 
       var q = 'FatController'; 
       var msg = 'Hello World!'; 

       ch.assertQueue(q, {durable: false}); 
       // Note: on Node 6 Buffer.from(msg) should be used 
       ch.sendToQueue(q, new Buffer(msg)); 
       console.log(" [x] Sent %s", msg); 
      }); 

      setTimeout(function() { 
       conn.close(); 
       process.exit(0) 
      }, 500); 
     } 
    }); 

}; 

module.exports = {push:push}; 

當我嘗試運行此我得到以下錯誤:

bundle.js:32074 TypeError: QS.unescape is not a function 
    at openFrames (bundle.js:9551) 
    at connect (bundle.js:9629) 
    at Object.connect (bundle.js:7959) 
    at Object.push (bundle.js:7652) 
    at controller.pushQueueEntry (bundle.js:7805) 
    at fn (eval at compile (bundle.js:32907), <anonymous>:4:184) 
    at callback (bundle.js:44543) 
    at Scope.$eval (bundle.js:35710) 
    at Scope.$apply (bundle.js:35810) 
    at HTMLInputElement.<anonymous> (bundle.js:44548) 
    at defaultHandlerWrapper (bundle.js:21283) 
    at HTMLInputElement.eventHandler (bundle.js:21271) 

我是不是找錯了樹這裏? amqplib只能運行在「正確的」節點環境中嗎?

作爲第二個問題,確定某個特定的NPM軟件包是否可以在瀏覽器環境中運行的最佳方法是什麼?在我看來,一些NPM包將在瀏覽器中運行,有些則不會 - 對此有信心的最佳方式是什麼?

回答

1

Will amqplib only run in a 'proper' node environment?

是的,我很害怕。

As a secondary question, what is the best way to determine whether a particular NPM package will run in the browser environment? It seems to me that some NPM packages will run in the browser, and some won't - what is the best way to be confident about this?

這並不總是很清楚,如果一個包可以在瀏覽器或無法運行,所以你必須運用一些啓發:

  • 難道一個包需要建立「普通」 TCP連接(意爲連接那些不常用的基於Web的協議,如HTTP(S)或WebSockets)?如果是這樣,它可能是一個服務器端包。
  • 包是否需要讀取任意文件?如果是這樣,再次,可能是服務器端。
  • 包是否提供或依賴於本地Node.js插件?如果是這樣,它是一個服務器端包。
  • 由於這些擴展:它使用fsnetclusterhttphttpstlsdnsosttydgram?最有可能的服務器端。

客戶端可以使用的包通常在其文檔中這樣說,所以如果沒有特別提及,那麼它很可能只是服務器端。

我沒有在一段時間內使用過Browserify,所以我不得不去檢查,但它看起來並不會警告你,如果你通過它的代碼取決於服務器端模塊。它會創建一個捆綁包,在某些時候只會失敗並出現錯誤,就像您正在運行的那樣。

Webpack是另一個常用的打包程序,其概念爲部署目標。默認情況下,它會針對瀏覽器,而當你試圖捆綁依賴於像我上面提到的那些服務器端模式的項目,你會得到一個錯誤:

$ webpack -p bundle.js 
Hash: 767ace79fc17abef93e8 
Version: webpack 2.6.1 
Time: 3983ms 
    Asset Size Chunks     Chunk Names 
bundle.js 308 kB  0 [emitted] [big] main 
    [0] <SNIP> 

ERROR in ./~/amqplib/lib/connect.js 
Module not found: Error: Can't resolve 'net' in '/private/tmp/node_modules/amqplib/lib' 
@ ./~/amqplib/lib/connect.js 152:11-25 
@ ./~/amqplib/channel_api.js 
@ ./test.js 

ERROR in ./~/amqplib/lib/connect.js 
Module not found: Error: Can't resolve 'tls' in '/private/tmp/node_modules/amqplib/lib' 
@ ./~/amqplib/lib/connect.js 155:11-25 
@ ./~/amqplib/channel_api.js 
@ ./test.js 

正如你所看到的,我的測試文件使用amqplib,這取決於nettls,它們在瀏覽器環境中都不可用。所以如果你不確定一個軟件包是否可以在瀏覽器中使用,Webpack會爲你提供一個安全網。

+0

非常有幫助和全面的答案羅伯特,非常感謝:) – Journeyman

相關問題