How To: Subscribe to stream services with ArcGIS API for Python
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>

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:
- Import the necessary modules.
from arcgis import GIS from arcgis.realtime import StreamLayer import ssl, json
- 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)
- 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)
- 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.
Related Information
- ArcGIS Pro: Python Package Manager
- ArcGIS Rest API: Stream Services
- ArcGIS API for Python: Overview of the ArcGIS API for Python
- Esri GitHub: arcgis.realtime module
Last Published: 7/31/2019
Article ID: 000020989
Software: ArcGIS Pro 2.3.3, 2.3.2, 2.3.1, 2.3, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2, 2.1.3, 2.1.2, 2.1.1, 2.1, 2.0.1, 2.0