Subscribing To Zone Events

This guide shows how to subscribe to new zone events using JavaScript.

Creating WebSocket Connection

// replace 192.168.225.2 with your server IP
var connection = new WebSocket("ws://192.168.225.2:8080"); 

connection.onmessage = function(message) {
	console.log(message.data);
};

Subscribing To All Zone Resources

// replace API_KEY with your secret API key
var subscriptionMessage = {
	headers: {
		"X-ApiKey": API_KEY
	},
	method: "subscribe",
	resource: "/zones/"
};

connection.send(JSON.stringify(subscriptionMessage));

Message Format

{
	body:{
		feed_id: "1", 						// ID of the tag interacting with zone
		zone_id: "1",						// ID of the zone
		status: "in",						// type of interaction "in" / "out"
		at: "2018-12-03 11:22:04.598000"	// timestamp
	},
	resource: "\/zones\/1"					// name of the zone resource
}

Unsubscribing From Zone Resources

// replace API_KEY with your secret API key
var unsubscriptionMessage = {
	headers: {
		"X-ApiKey": API_KEY
	},
	method: "unsubscribe",
	resource: "/zones/"
};

connection.send(JSON.stringify(unsubscriptionMessage));

Full documentation of WebSocket integration you can find in section Websocket API.

On this page: 


More about WebSocket API in section API documentation.