Up to this point all the activities have been using the HTTP Protocol, which uses a request-response process (the client requests a resource and the server responds with this resource). If this seems unfamiliar you should work through the HTTP Protocol worksheet.
Whilst this approach works fine for delivering content to a web browser it is not a useful approach for certain applications. Imagine a chat room where you had to refresh the page to view new messages.
In this worksheet you will learn how to use a new HTML5 websocket protocol that allows a full duplex (2 way) communication over a single TCP connection. We will then explore the MQTT protocol which can be run over websockets and is used to implement a push message system, technically called publish-subscribe.
Start by installing the Mosquitto Tools.
If you are using MacOS you should install the Brew Package Manager and use this to install Mosquitto using brew install mosquitto
.
mosquitto_sub -h test.mosquitto.org -t "#" -v
Now we have the tools installed we can start using the protocol. You have installed 2 tools, mosquitto_pub
is used to publish messages and mosquitto_sub
subscribes to a channel. We will use the test.mosquitto.org
broker.
Start by opening two terminal windows:
In the first window we will run the mosquitto_sub
command and subscribe to a topic called 205CDE/XXX
where XXX
is your university username.
$ mosquitto_sub -h test.mosquitto.org -t 205CDE/XXX
- The
-h
flag allows us to specify the host, in this casetest.mosquitto.org
. - The
-t
flag allows us to specify the topic, in this case205CDE/XXX
(remember to substitute your username)
In the second terminal we will run the mosquitto_pub
command to publish messages to our topic.
$ mosquitto_pub -h test.mosquitto.org -t tyers/mark -m 'hello world'
- The
-h
flag allows us to specify the host, in this casetest.mosquitto.org
. - The
-t
flag allows us to specify the topic, in this case205CDE/XXX
(remember to substitute your username) - The
-m
flag allows us to specify the message, in this casehello world
.
If you look at the first terminal window (running mosquitto_sub
) you should see your message displayed.
Working in small groups of between 2 and 4 people:
- Decide on the topic name you will use.
- Everyone runs the
mosquitto_sub
tool and subscribes to this same topic. - Each person launches a new terminal in a new pane (so you can see both terminal windows).
- use the
mosquitto_pub
tool to send a message to your chosen topic name. - Look at the output of your
mosquitto_sub
command (in the first terminal window).
What have you produced? Can you think of any application for this...
Open the subscribe.js
file and study the contents:
- When you run the script notice that the title is displayed in colour by passing 2 parameters to
console.log()
, the first parameter defines the colour:- The string before the
%s
string defines the colour to be used. There is a reference that defines these. - The string aftet the
%s
string resets the colours to default values so that any subsequent text reverts back to the defaults.
- The string before the
- We import the
mqtt
package and use this to create a new client object by calling theconnect()
function and passing in the URL.- Notice we specify the MQTT protocol rather than HTTP, by specifying
mqtt://
. - We are connecting to the
test.mosquitto.org
server.
- Notice we specify the MQTT protocol rather than HTTP, by specifying
- The
client.on()
function allows us to call anonymous functions to respond to key events:- Once we have connected we subscribe to one or more topics.
- When a message is received we have a callback that passed both the topic and message as parameters.
- The
\t
character combination inserts a tab character.
- Change the script so it subscribes to your group's chosen topic. Run it to make sure it is receiving the messages.
- Subscribe to an additional topic called
205CDE/announcements
and use themosquitto_pub
tool to publish some messages to the entire class! - Modify the script so that the topic names are dimmer than the messages.
- Modify the script so that any messages published to the
205CDE/announcements
are coloured in red.
https://github.com/jpmens/simple-mqtt-websocket-example
Web Sockets is a bidirectional communication technology which operates over a single socket. It is implemented using a JavaScript object called WebSocket
. This can be used to send data to a server and receive data through an event handler.
We start by creating a new WebSocket object passing the URL of the server.
const ws = new WebSocket(url)
This object contains a readyState
property that represents the state of the connection. It contains one of four different values.
| value | meaning | | ----# | ----------------------------------------------------------- | | 0 | the connection has not yet been established | | 1 | the connection is established and communication is possible | | 2 | the connection is going through the closing handshake | | 3 | the connection has been closed or could not be opened |
It has two functions:
- Data can be sent to the server using
WebSocket.send()
. - The connection can be terminated using
WebSocket.close()
.
There are four events associated with the WebSocket
object.
event | event handler | description |
---|---|---|
open |
Socket.onopen |
socket connection is established |
message |
Socket.onmessage |
client receives data from server |
error |
Socket.onerror |
error in communication |
close |
Socket.onclose |
connection is closed |