Muon in the browser.
See http://muoncore.io for more information on muon
To test drive muon.js, you can run the demo application. This starts a gateway, and demonstrates the capabilities coming from running muon in the browser.
You’ll need two dependencies:
-
node.js installed so you can use npm: https://nodejs.org/en/download/
-
RabbitMQ Running locally we suggest using our handy muon development VM here: https://github.com/muoncore/muon-dev-vm
npm install
make
make run
Visit http://localhost:5000 for a running muon instance in the browser. It is given full permissions to communicate with the local muon environment.
Start a few other muon services, especially event services, such as Photon.
You will need to implement both a server side gateway, and a client browser application.
A websocket based transport message router is required for muon instances in the browser to connect to.
A prebuilt gateway is available at https://github.com/muoncore/muonjs-gateway.js, and this is recommended for use.
This is an advanced task, for very tight control of the message flows from browser to server side.
See the existing gateway. The only currently supported server side setup is node.js with Express.
var http = require("http")
var express = require("express")
var app = express()
var port = process.env.PORT || 5000
app.use(express.static("./test/server"))
var server = http.createServer(app)
server.listen(port)
var Muon = require("muon-core"); // (1)
var muon = Muon.create("browser-gateway", process.env.MUON_URL || "amqp://muon:microservices@localhost"); // (2)
var muonjs = require("muon.js").gateway(server, muon) // (3)
-
Import Muon Core.
-
Create a new Muon service, the local gateway. This will be used to communicate with the rest of the system
-
Create a Muon.js browser gateway. Muon browser clients will communicate with this.
That’s it!
Muon.js is published to NPM. Include it into your application using require and use a packager, such as browserify or webpack to transpile it into your application code.
Here is a minimal browser javascript script.
This instantiates a muon instance, which will connect to the gateway you created above.
var muon = require("muon.js").client() // (1)
setInterval(function(){
var then = new Date().getTime();
muon.request("rpc://myservice/endpoint", {"message": "BE AWESOME"}, function(resp) { (2)
var now = new Date().getTime();
logger.info("Latency = " + (now - then))
console.dir(resp) //(3)
});
}, 2000)
-
Instantiate a muon.js client. This auto connects to the gateway using websockets.
-
Every two seconds, make an rpc request to myservice
-
Process the response, logging it to the browser javascript console.
You will need to process this into something you can import, using browserify or webpack.