项目作者: sjmf

项目描述 :
Angular 2 demo using MQTT.js in Typescript
高级语言: TypeScript
项目地址: git://github.com/sjmf/ng2-mqtt-demo.git
创建时间: 2016-10-30T19:04:58Z
项目社区:https://github.com/sjmf/ng2-mqtt-demo

开源协议:MIT License

下载


MQTT Angular 2 Demo App

A demo application using Angular 2,
Typescript, and
mqtt.js. This project uses
angular-cli
and is a re-write of my earlier example using stomp.js
(where I really should have foreseen that the transport doesn’t need to
be tied to one library. Well, you live and learn.)

This app demonstrates a more ng2-faithful way of connecting to a message
queue and subscribing to messages via MQTT. It includes an MQService
which subscribes to messages, and an example ‘raw data’ component which
uses the Observable type to data-bind messages to the DOM.

Quick Start

As well as the following, you will also need the appropriate toolchain for
Typescript, and a message queue supporting MQTT, such as ActiveMQ.

To get started running this app locally, install angular-cli
and the necessary dependencies:

  1. # Clone the repo and cd into it
  2. git clone https://github.com/sjmf/ng2-mqtt-demo
  3. # cd into it
  4. cd ng2-mqtt-demo
  5. # Install the packages from package.json
  6. npm install

You will also need to edit the src/api/config.json configuration
file to set the correct connection parameters for your message broker.
This file is included with the idea that in a production app, you might
want to get credentials for a connection from an API. See the
angular-cli instructions for proxying to a backend
for more information.

When you’ve done this, you can run the application locally: run ng serve
for a dev server. Navigate to http://localhost:4200/. The app will
automatically reload if you change any of the source files.

Layout

The source is located under the app folder. Partial tree is below:

  1. ├── src * Source folder
  2. ├── api * Example API folder (static for demo)
  3. └── config.json * Configuration file for STOMP
  4. ├── app * Application folder
  5. ├── components * Components folder
  6. ├── rawdata * Example data streaming component folder
  7. └── status * STOMP Status component folder
  8. ├── pipes * Pipes folder
  9. ├── services * Services folder
  10. ├── config * Config service folder (retrieves the configuration)
  11. └── mqtt * MQTT service folder (ng2 definition for an MQTT configuration)
  12. ├── app.component.css * Component css file
  13. ├── app.component.html * Component html file
  14. ├── app.component.spec.ts * Component testings
  15. ├── app.component.ts * Top-level app-root component
  16. ├── app.module.ts * App module definition
  17. └── index.ts * Indexing file
  18. ├── assets * Assets folder
  19. └── .gitkeep * Placeholder to include the folder to source control
  20. ├── environments * Environment settings folder
  21. ├── environment.prod.ts * Production environment settings
  22. └── environment.ts * Development environment settings
  23. ├── index.html * The root page served to browser
  24. ├── main.ts * App bootstrap
  25. ├── polyfills.ts * Polyfills file
  26. ├── styles.css * Main css file
  27. ├── tsconfig.app.json * App Typescript transpiler options
  28. └── typings.d.ts * Typescript typings definition file
  29. ├── .angular-cli.json * Angular CLI configuration file
  30. ├── package.json * Package info and list of dependencies to install
  31. ├── tsconfig.json * Main Typescript transpiler options
  32. └── tslint.json * Typescript Linter configuration file

Excluded from this partial tree for brevity: sub-component .ts .html etc
files under folders, testing framework files, and the e2e End to End testing
folder containing app behaviour testings and definitions. The node_modules
directory will also be generated for the installed node packages.)

Extending

The example data streaming component provides a demonstration of how to use the
MQService to subscribe to a data stream. At its’ core, the MQService makes
available an Observable which the RawDataComponent uses in its own template,
and additionally subscribes its’ own on_next method to.

A barebones set-up of the service could run from a component’s ngOnInit
method, and might look something like this:

  1. this._mqService.configure( config, () => console.log("connected") );
  2. this._mqService.try_connect();

Our RawDataComponent then copies a reference to the public member messages,
which can be used with a template variable and the |async pipe to update the
template in real time.

The instantiating component must provide an instance of MQService. This
implementation also uses a ConfigService to retrieve the MQTT connection
variables from a json file, with the intention that other clients might like to
route this request to an API along with some form of user token.

The MQTT connection status is also fed-back to the application user via a
BehaviorSubject observable, implemented following the model used in
this Angular2 stocks app. If the connection
fails, the application will retry every 5 seconds until it reopens.

Contributing

Very happy to accept suggestions for improvement (or even pull requests!).
Just raise an issue and let me know :) - I’ll try and work on it when I
get time.

Licence

MIT Licence.