Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346399
  • 博文数量: 102
  • 博客积分: 3140
  • 博客等级: 中校
  • 技术积分: 680
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-28 11:44
个人简介

开阔未来

文章分类

全部博文(102)

文章存档

2015年(10)

2014年(1)

2013年(1)

2012年(4)

2011年(8)

2010年(24)

2009年(51)

2008年(3)

我的朋友

分类: 系统运维

2012-01-03 19:48:51


For a while I’ve been looking at how to bridge the and websockets to make it easier to build web applications using data broadcast in MQTT streams. In the past I used python and mod_pywebsocket along with mosquitto python libraries however this was cumbersome and difficult to install.  Here I present a simple solution using to interact with MQTT clients. Node.js lends itself to working well with messaging systems like MQTT and websockets due to its event driven nature. I’m also in love with node.js at the moment!

This is much simpler than previous attempts and I put the initial test together in less than ten minutes.

Prerequisites

Obviously you’ll need to have and  installed and also the node library (which can easily be installed using ). All my work was tested under ubuntu but there is no reason why it wouldn’t work on OSX or even cygwin. To test you’ll need a websocket compatible browser such as recent versions of chrome.

System Structure

mosquitto_pub and mosquitto_sub are command line MQTT clients supplied with the mosquitto MQTT broker, here mosquitto_sub will be called using node.js as child processes, events are generated on output from the process. The data is then captured and broadcast over a websocket. In this case a simple jquery page is used to display the broadcast messages but in a real application there would be more client side processing to make a useful application.

Simple Server Side Subscriber Code

This example subscribes to the topic “test” and broadcasts all messages on this topic over the websocket on port 8000.

1/* Include required libraries */
2var util   = require('util'),
3sys = require("sys"),
4ws = require("websocket-server"),
5spawn = require('child_process').spawn,
6 
7/* Create websocket server */
8server = ws.createServer({debug: true}),
9 
10/*
11* Ceate call to mosquitto_sub cli client
12* to subscribe on topic "test"
13*/
14mosq = spawn('mosquitto_sub',['-t','test']);
15 
16/*
17* Bind an event to stdout which occurs when
18* mosquitto_sub outputs anything
19*/
20mosq.stdout.on('data', function (data) {
21/*
22* Brodcast the MQTT message straight back
23* out on the websocket
24*/
25server.broadcast(data);
26/* Log the message to the console */
27console.log('' + data);
28});
29 
30/*
31* Bind an event to stderr so we can see any
32* errors that cause mosquitto_sub to crash
33*/
34mosq.stderr.on('data', function (data) {
35console.log('error: ' + data);
36});
37 
38/* Start the websocket server listening on port 8000 */
39server.listen(8000);
Demonstration Client

This is a bare bones page which prints out messages received over the websocket.

1
2
3
4 
5
6
7    
8    
9 
10
11 
12
31
32
Further Development

This example is of little practical use on its own however it could easily be the basis of a flashy home monitoring system. Publishing MQTT messages could be easily achieved by calling mosquitto_pub in the child process.

Tags: , , , , , , , ,

Node.js, MQTT and Websockets
阅读(2292) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~