Retrieving incidents using the Websocket (real-time) API
To connect to the Incident WebSockets you need to connect to this url: wss://www.fireservicerota.co.uk/cable?access_token={access_token} and
connect to the channel called "IncidentNotificationsChannel" for the specific station you wish to receive updates for.
FireServiceRota uses Rails/ActionCable. These are plain websockets, but use a specific format to subscribe to a channel. See below for example code in Javascript. All major languages have production-ready websocket libraries.
The WebSockets returns the same exact format as the incident API.
// Opens a WebSocket connection to a specific stream.
function openWebsocketConnection() {
// Creates the new WebSocket connection.
socket = new WebSocket(urlWithAccessToken);
socket.onopen = function(event) {
console.log('WebSocket is connected.');
const msg = {
command: 'subscribe',
identifier: JSON.stringify({
channel: 'IncidentNotificationsChannel',
// station ID for which you want updates for. Omit if any.
group_id: String(groupId)
}),
}; socket.send(JSON.stringify(msg));
};
// When the connection is closed, this code will run.
socket.onclose = function(event) {
console.log('WebSocket is closed.');
};
// When a message is received through the websocket, this code will run.
socket.onmessage = function(event) {
const response = event.data;
const msg = JSON.parse(response);
// Ignores pings.
if (msg.type === "ping") {
return;
}
console.log("FROM RAILS: ", msg);
// Do something with msg
};
// When an error occurs through the websocket connection, this code is run printing the error message.
socket.onerror = function(error) {
console.log('WebSocket Error: ' + error);
};
}
Comments
0 comments
Article is closed for comments.