HOW TO

Subscribe to stream services with ArcGIS API for Python

Last Published: April 25, 2020

Summary

In ArcGIS API for Python, the Jupyter Notebook can be used to visualize real-time data. This is possible using the arcgis.realtime Python module. Within the module, use the subscribe() method with the prerequisite packages installed on the machine.

Procedure

To use the subscribe() method, the following packages are required:

  • twisted
  • autobahn
  • pyOpenssl
  • service_identity

The packages can be installed using the Python Command Prompt. Run the following command to install the selected packages individually.

conda install <package name>
The image of the Python Command Prompt with sample command to install twisted
Note:
Some of the packages may have been installed during the ArcGIS API for Python installation. To check the installed packages, refer to ArcGIS Pro: Python Package Manager.

The following steps demonstrate how to subscribe to stream services:

  1. Import the necessary modules.
from arcgis import GIS
from arcgis.realtime import StreamLayer
import ssl, json
  1. Create the SSL connection to the stream services.
ssl._create_default_https_context = ssl._create_unverified_context
url = "https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer"
streamLayer = StreamLayer(url)
  1. The subscribe() method requires a callback function to operate. Define a callback function.
def callback(event):
    event_as_json = json.loads(event)
    print(event_as_json)
  1. Execute the subscribe() function.
streamLayer.subscribe(callback)
The following is a sample of the full script:
from arcgis import GIS
from arcgis.realtime import StreamLayer
import ssl, json
 
ssl._create_default_https_context = ssl._create_unverified_context
url = "https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer"
streamLayer = StreamLayer(url)
 
def callback(event):
    event_as_json = json.loads(event)
    print(event_as_json)
 
streamLayer.subscribe(callback)
Note:
The code may not run as expected in a Jupyter Notebook if it is terminated midway. If this happens, restart the notebook kernel and rerun the code.

Article ID:000020989

Software:
  • ArcGIS Pro
  • ArcGIS API for Python

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic