The MQTT messaging client for JavaScript and web apps
How using a web app eases application deployment
- The system administrator installs or embeds the client library.
- The system administrator arranges for the messaging app to be distributed to the end users and installed on their local systems.
- When the code changes, the system administrator repeats the previous steps (so change management is complex).
- The system administrator serves the web app and the client library at a URL.
- The end user's browser pulls in the web app and client library together.
- When the code changes, the updated version is picked up when the browser is refreshed (so change management is simple).
Why you might want to use messaging directly from the browser in your web apps
- If you send and receive messages through a messaging system, that system is responsible for ensuring that the messages are delivered.
- Because the messaging system looks after delivery, your web app can "fire and forget". This greatly simplifies the programming logic. If messages are delivered for you, your code need not check they got there. Your app no longer needs to handle receipt acknowledgement, or save undelivered messages and retry them later.
- Messaging systems provide event-driven messaging. Your client app no longer needs to send a request then continuously poll for a response. Instead, the messaging server sends a message to your client app when an interesting event occurs. This also means that your client app is alerted as soon as the event happens, rather than waiting until the next time the app polls the server.
- Event-driven messaging also massively reduces the load on the device hosting your client app, the network traffic between the browser and the messaging server, and the load on the messaging server. This matters increasingly, as more and more systems are running on mobile devices, and connecting across wireless networks.
How the pieces fit together
The MQTT messaging client for JavaScript includes a client library, and an example web app that uses the library. You code your own web app that uses the library. The web app and client library are then made available at your chosen URL, for example by an MQ queue manager (as in the following diagram) or by an application server. The browser pulls in the web app and client library, and the web app then uses the browser to connect to, and exchange messages with, an MQTT server such as IBM WebSphere MQ Telemetry or IBM MessageSight.
- Each instance of the browser refreshes its connection to the URL on which the web app is available, and an up-to-date version of the web app and client library is loaded onto the browser.
- The web app connects to a queue manager, using MQTT over the WebSocket protocol, and subscribes to a topic of interest.
- The queue manager uses the same connection to send messages that match the subscription back to the web app.
The web app contains application logic, and the URL of the MQTT server. When opened in a browser, the app connects to the MQTT server, creates the subscriptions it needs, then waits to receive event-driven alerts and act on them.
The web app connects using
MQTT as the transport protocol, running over WebSockets. Most modern
browsers can make WebSockets connections.
By using WebSockets,
the web app can pass messages through firewalls that accept HTTP and
the WebSocket protocol,
and can send packets of data (known as frames
) just like using
TCP over IP.
When a message sent by the web app arrives in the MQTT server, the server-side app just sees it as a message. It does not know the message has come from a browser.
Administering and controlling an MQTT server
- Create a server.
- Pick a port.
- Define a new MQTT channel.
- Configure your client web app to connect to the chosen port across the new MQTT channel.
- Create a server.
- Pick a port (1883 by default).
- Define a new MQTT channel.
- Configure your client web app to connect to the chosen port across the new MQTT channel.
MQINSTALL
|
mqxr
| |
| SDK
| |
| WebContent
| sample web app (the Web Messaging Utility
sample HTML pages)
| |
| WebSocket
| IBM client library (the messaging client JavaScript classes)
|
qmgrs
|
qmgr_name
|
mqxr
|
WebContent
|
your_client_app (your own JavaScript pages)
The
sample web app and client library are stored in the MQINSTALL/mqxr/SDK/WebContent directory.
Material in this directory is served by all queue managers. If you
do not want your users to see and use all this material, you should
create your own version of the app. To make this app, or your own
replacement app, available on specific queue managers, you put the
app in the MQINSTALL/qmgrs/qmgr_name/mqxr/WebContent directory.
To select the app and associated JavaScript classes to serve
at a URL, the queue manager looks first in its own WebContent directory,
then in the global WebContent directory. In the
previous example directory tree, the queue manager serves your_client_app and
the global copy of the JavaScript classes.To stop the queue manager serving the web app executable files, or modify where the queue manager looks for the executable files, you configure the webcontentpath property and add it to the mqxr.properties file. See MQXR properties.