Skip to content

muoncore/muon.js

Repository files navigation

Muon JS

Muon in the browser.

See http://muoncore.io for more information on muon

Getting Started

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.

Dependencies

You’ll need two dependencies:

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.

Adding to your application

You will need to implement both a server side gateway, and a client browser application.

Gateway

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.

Building Your own Gateway

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)
  1. Import Muon Core.

  2. Create a new Muon service, the local gateway. This will be used to communicate with the rest of the system

  3. Create a Muon.js browser gateway. Muon browser clients will communicate with this.

That’s it!

Client

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)
  1. Instantiate a muon.js client. This auto connects to the gateway using websockets.

  2. Every two seconds, make an rpc request to myservice

  3. 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.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published