Polling Example with Websockets

Continueing from an earlier article, we are going to show how to use the Eagle Eye Polling API with Websockets.

Websockets are a bidirectional HTTP connection that behaves like a traditional network socket. It starts out as traditional HTTP Request but passes a header indicating that it would like to upgrade to a Websocket. This is a common and well supported option for modern webapps. It is more efficient than the previous polling example because it does not create and destroy the connection for every request.

Most common programming languages has the ability to support websockets. I will show two examples using Python and Node.js. Both of there projects are available as examples. They can be installed or run in their Docker containers.

Getting Started

The best place to start is with the API documentation. This explains the details of the HTP request, handshake, and other important caveats.

The Status Bitmask will be covered in a separate article, for now, I recommend focusing on the parsed status output returned by these examples. The parsed status output matches the status labels used inside of the VMS.

Example 1: Python script

This script with go through the steps of logging-in, getting devices, subscribing to the poll stream and parsing the status information. The output is formated to give a human readable output that shows the status of camera on/off, camera internet online/offline, camera streaming, and camera recording.

The script will maintain the Websocket until it is interrupted. To keep this example simple, I am not showing of to handle disconnections and errors. In production, the script should handle erros and reconnect when needed.

You can download this script here.

Example 2: Node.js

This project subscribes to the Poll stream with Websockets, addes events that match the set criteria to a queue, and then it can be configured to do something in the `worker.js` file. The most common use case is to have it call a webhook and pass the data to another service. This project is intended to be a starting point and not an end-to-end example.

Inside of `config.js` thre are several parameters that need to be configured. The first is username, password, and API key. After that, it is important to specify the events you want the script to listen for. Listening for recording start/stop events can potentially cause a lot of events to be enqueued.

You can download this project here.

Working with multiple sub-accounts

If you login as a user at the dealer level, you will need to switch into a subaccount before you can access the devices for that subaccount. You can do this by getting a list of subaccounts you have access to and then makeing a switch account call.

Each sub-account will need a subscription to the poll stream for the devices in that account. This means that you will need to manage having one websocket per subaccount. This can be done through threads or by making the process capable of using multiple processes.

Maintaining a connect over the internet is going to experience interrrupts and the occasional dropped connection. It is important that before going into production, these events are handled and connections are re-established.

What else can we do with this?

I hope that this is a helpful starting point as you explore this feature of the Eagle Eye API. Getting real time information is key in many integrations and with these examples you should be able to quickly adapt them for your needs. I hope you found this helpful. If you have any questions please feel free to reach out to us at api_support@een.com