Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Stream Device Data with a WebSocket (Use Case)

In this example, we will discuss the Juniper Mist use case of streaming device data with a WebSocket.

A WebSocket is a protocol that provides full-duplex communication over a TCP connection. A WebSocket API provides a way for a client to communicate with an endpoint. Juniper Mist uses this protocol to stream near-real-time data to a client. A client will make a request for the data it wants to receive by subscribing to a channel. The client makes the request only once, and the server will stream the channel data to the client as updates are made.

This method of communication is good for receiving event-driven data in near–real time. Its primary use is for collecting device data. You and other network administrators can then feed this data into a custom event display, a notification system, an external logging facility, and much more.

Using WebSocket is especially helpful if the information that your organization needs isn’t available natively within the Juniper Mist portal.

In this use case, we show how to configure a client to request and subscribe to a site’s device statistics.

Using a WebSocket requires the following elements:

  • Authentication

  • HTTP header configuration

  • WebSocket Connection URL (wss://api-ws.mist.com/api-ws/v1/stream). Please verify your URL based on your geographical location.

Communicate with a MIST WebSocket Endpoint

To communicate with the Juniper Mist WebSocket endpoint, you use an application called Postman. Postman is a GUI API platform for building and using APIs. You can build scripts that make multiple API calls. This API also enables you to make WebSocket calls.

The following steps describe how you can connect to the Juniper Mist WebSocket, starting with adding the URL to the input box next to the Connect button:

  1. Click Connect.
    The Messages pane shows that the client tried to connect and was immediately disconnected. This is because the Juniper Mist WebSocket requires authentication.
  2. To fix the authentication problem, add a custom HTTP header to the request.
    1. Under the KEY heading, create a new “Authorization” key.

    2. In the VALUE input box, enter the word token followed by a space and then the Juniper Mist authorization token.

    3. Select the check box on the left of the KEY column to make it active.

    4. Click Connect to establish the WebSocket session with Mist.

  3. Once connected, switch to the Message heading in Postman.

    This is where you tell Mist which data you want, by subscribing to the device statistics stream.

    The following example requests device statistic data for the specified site.

  4. With this information entered in the Messages input box, click Send.

    Postman presents your request with an Up Arrow and the Juniper Mist response with a Down Arrow.

    The following example shows what you can expect to see in a response from Juniper Mist.

    Within a few seconds of subscribing, you should start to see events streaming into the Messages pane in JSON format (default):

    You can see the same output in the following formats:

    • Text

      1. {"event": "data", "channel": "/sites/c1947558-268d-4d31-xxxx-xxxxxxxxxxxx/stats/devices", "data": "{\"mac\": \"5c5b35fxxxxx\", \"last_seen\": 1686592607, \"uptime\": 6259614, \"version\": \"0.9.22801\", \"_partition\": 48, \"_offset_apbasic\": 4639768722, \"ip_stat\": {\"dns\": [\"10.10.12.11\", \"10.10.12.12\"], \"ips\": {\"vlan12\": \"10.10.12.25/25,fe80:0:0:0:5e5b:35ff:fef1:5ed8/64\"}, \"gateway\": \"10.10.12.1\", \"ip6\": \"fe80:0:0:0:5e5b:35ff:fef1:5ed8\", \"netmask6\": \"/64\", \"ip\": \"10.10.12.25\", \"netmask\": \"255.255.255.128\", \"dhcp_server\": \"10.10.12.1\"}, \"ip\": \"10.10.12.25\", \"ble_stat\": {\"tx_pkts\": 9073, \"tx_bytes\": 105431, \"rx_pkts\": 393432193, \"rx_bytes\": 2772782221, \"tx_resets\": 0}, \"_time\": 1686592607.62131}"}

    • HTML

      1. {"event": "data", "channel": "/sites/c1947558-268d-4d31-xxxx-xxxxxxxxxxxx/stats/devices", "data": "{\"mac\":

      2. \"5c5b35fxxxxx\", \"last_seen\": 1686592607, \"uptime\": 6259614, \"version\": \"0.9.22801\", \"_partition\": 48,

      3. \"_offset_apbasic\": 4639768722, \"ip_stat\": {\"dns\": [\"10.10.12.11\", \"10.10.12.12\"], \"ips\": {\"vlan12\":

      4. \"10.10.12.25/25,fe80:0:0:0:5e5b:35ff:fef1:5ed8/64\"}, \"gateway\": \"10.10.12.1\", \"ip6\":

      5. \"fe80:0:0:0:5e5b:35ff:fef1:5ed8\", \"netmask6\": \"/64\", \"ip\": \"10.10.12.25\", \"netmask\": \"255.255.255.128\",

      6. \"dhcp_server\": \"10.10.12.1\"}, \"ip\": \"10.10.12.25\", \"ble_stat\": {\"tx_pkts\": 9073, \"tx_bytes\": 105431,

      7. \"rx_pkts\": 393432193, \"rx_bytes\": 2772782221, \"tx_resets\": 0}, \"_time\": 1686592607.62131}"}

    • XML

      1. {"event": "data", "channel": "/sites/c1947558-268d-4d31-xxxx-xxxxxxxxxxxx/stats/devices", "data": "{\"mac\": \"5c5b35fxxxxx\", \"last_seen\": 1686592607, \"uptime\": 6259614, \"version\": \"0.9.22801\", \"_partition\": 48, \"_offset_apbasic\": 4639768722, \"ip_stat\": {\"dns\": [\"10.10.12.11\", \"10.10.12.12\"], \"ips\": {\"vlan12\": \"10.10.12.25/25,fe80:0:0:0:5e5b:35ff:fef1:5ed8/64\"}, \"gateway\": \"10.10.12.1\", \"ip6\": \"fe80:0:0:0:5e5b:35ff:fef1:5ed8\", \"netmask6\": \"/64\", \"ip\": \"10.10.12.25\", \"netmask\": \"255.255.255.128\", \"dhcp_server\": \"10.10.12.1\"}, \"ip\": \"10.10.12.25\", \"ble_stat\": {\"tx_pkts\": 9073, \"tx_bytes\": 105431, \"rx_pkts\": 393432193, \"rx_bytes\": 2772782221, \"tx_resets\": 0}, \"_time\": 1686592607.62131}"}

    You will continue to receive messages until you either disconnect or unsubscribe from the channel by sending a message.

    Once you receive the data, you can do any number of things with it, such as:

    • Create a custom display or dashboard of events.

    • Archive the data long term.

    • Create custom monitoring and alerts.

    • Create more automations based on results.

    For examples using other automation tools to communicate with a MIST WebSocket endpoint, such as Python, see Automation Tools.