Licence
MIT
Version
0.6.1
Deps
7
Vulns
6
Weekly
0
Brokr
brokr is a javascript RPC+PUB/SUB via Websocket library that you shouldn't use yet.
If you want to anyway, go get it with npm install brokr
I am quite serious with my versioning of this, so please install a specific version to avoid breaking your application.
I follow Semantic Versioning 2.0.*
Usage
In the browser
If you need to use Brokr in the browser, you can
- Use the included connect middleware
Brokr.middleware(url)to serve a browser friendly Brokr version. - Use the
Brokr.browserify(callback)function to get the source as a string. - Use the included build tool. Simply run
make buildin the Brokr module root to build thebrokr.web.jsfile.
Note that i am writing a better more optimised version exclusively for the browser, and that the current browser implementation works, but have a gigantic footprint.
The browser version will have it's own repository, will properly not be avaiable through npm.
Example with connect
Your node application (lets call it app.js):
var Brokr = require('brokr')
, connect = require('connect');
//let us choose a reasonable port number
var port = 4000;
//Start a http.Server as you usually would
//Here we are using connect() to take advantage of
//brokr's built in middleware for serving the static client
var server =//our http.Server instance as returned by connect's .listen() method
connect()
//serve the brokr source on requests to /brokr.js
.use(Brokr.static('/brokr.js'))
//serve the "public" directory as is (put your index.html here)
.use(connect.static(__dirname+'/public'))
//and start the webserver
.listen(port);
console.log("listening on port "+port);
//create a new Brokr object, exposing a single method called ping
var brokr = new Brokr({
ping: function (response) {
//when client requests ping, send with no error and the string "pong"
response(null, "pong");
}
});
//attach our brokr to our server instance using the .listen() method
//The listen method could also take a port number which would start
//a new server on that port
brokr.listen(server).then(function(){
console.log("brokr listening on port "+port);
}).done();
Your ./public/index.html file:
<!DOCTYPE html>
<html>
<head>
<title>Brokr web demo</title>
</head>
<body>
<!-- request the brokr script -->
<script src="/brokr.js"></script>
<script>
var brokr = new Brokr();
//connect to the server
brokr.connect('ws://'+window.location.host)
.then(function(connection){ //then, on connect
//return a promise for a ping response
console.log("connected");
return connection.request('ping');
})
.then(function(response){ //then on response
console.log("got ping response,", response);
})
.then(function(){ //then return a promise to disconnect
console.log("disconnecting..");
return brokr.disconnect();
})
.done(); //and we are done
</script>
</body>
</html>
