Skip to content

Instantly share code, notes, and snippets.

@jan-osch
Last active February 13, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jan-osch/2b079c398903bcca3edb2f65124e04c7 to your computer and use it in GitHub Desktop.
Save jan-osch/2b079c398903bcca3edb2f65124e04c7 to your computer and use it in GitHub Desktop.
const io = require('socket.io-client');
const request = require('request-promise-native');
// You can find these credentials in Estimote Cloud UI
// Go to https://cloud.estimote.com/#/apps
const APPLICATION_ID = 'YOUR_APP_ID_HERE';
const APPLICATION_TOKEN = 'YOUR_APP_TOKEN_HERE';
// Fetch location ids that interest you using following curl
// curl -X GET 'https://cloud.estimote.com/v3/locations' -u APPLICATION_ID:APPLICATION_TOKEN -H "Accept: application/json"
// Use "id" field from "data"
const FIRST_LOCATION_ID = 'YOUR_FIRST_LOCATION_ID_TO_WATCH_HERE';
const SECOND_LOCATION_ID = 'YOUR_SECOND_LOCATION_ID_TO_WATCH_HERE';
// Fetch real-time connection details via HTTP request
const getConnectionDetails = () => request({
url: 'https://cloud.estimote.com/v2/analytics/real-time/room',
json: true, // response will be parsed as JSON
auth: {
user: APPLICATION_ID,
pass: APPLICATION_TOKEN,
},
});
const takeActionInFirstLocation = (uniqueVisitorsCount) => {
if (uniqueVisitorsCount > 100) {
console.log('A lot of visitors in first location');
console.log('Sending more staff to first location');
}
};
const takeActionInSecondLocation = (uniqueVisitorsCount) => {
if (uniqueVisitorsCount < 10) {
console.log('Second location is quite empty');
console.log('Sending some staff from second location back to headquarters');
}
};
const startWatchingRealTimeData = async () => {
const { room_id, url, path } = await getConnectionDetails(); // first your room_id and connection details
console.log('successfully fetched real-time connection details');
const socket = io(url, { path, transports: ['websocket'] }); // initialize socket connection
socket.on('connect', () => {
console.log('socket connection established');
socket.emit('room', room_id); // join your room to listen for events for your account
});
socket.on('visitors', ({ count, type, location_id }) => { // listen to visitors events
if (type === 'visitors_in_location' && location_id === FIRST_LOCATION_ID) { // filter visitors in specific location
takeActionInFirstLocation(count);
}
if (type === 'visitors_in_location' && location_id === SECOND_LOCATION_ID) { // filter visitors in specific location
takeActionInSecondLocation(count);
}
});
};
startWatchingRealTimeData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment