Select to view content in your preferred language

Has anyone used the arcgis.realtime.StreamLayer?

06-20-2019 08:39 AM
New Contributor

I am trying to build a Python script to pull off features from a GeoEvent streaming service but I have not found ANY examples of how to use the StreamLayer class. Not even Esri has samples for how to use it.

0 Kudos
1 Reply
Esri Regular Contributor

Hi Weldon,

You're in luck - I actually wrote on article on this exact topic not too long ago. I believe our content team is still working to make it public-facing, but I'll paste a copy for you below. This is a simple example that utilizes one of our sample GeoEvent Servers. The events are printed for illustrative purposes, but you may of course modify the function to process the received events however you wish:

In order to use the subscribe method, you must have the following packages installed: autobahn, twisted, pyOpenssl, and service_identity.

If you already have the ArcGIS API for Python installed, then you may already have some of the packages installed and just need to install twisted and autobahn:

conda install twisted
conda -c conda-forge install autobahn

The subscribe method expects at minimum a callback function. The below sample illustrates how you can define a callback function that print events (JSON) as they come in. This is a very basic example - you could further manipulate the JSON to view only certain information or even perform additional processing/data analysis if desired.

from arcgis import GIS
from arcgis.realtime import StreamLayer
import ssl, json
ssl._create_default_https_context = ssl._create_unverified_context
url = ""
streamLayer = StreamLayer(url)
def callback(event):
    event_as_json = json.loads(event)

Note: This script may not run well in a Jupyter Notebook - if you run the code and decide to stop the script, the next time you attempt to run the script you may encounter errors like these:

--------------------------------------------------------------------------- ReactorNotRestartable                     Traceback (most recent call last) <ipython-input-2-7cd591176ebd> in <module>      10     print(event)      11  ---> 12 streamLayer.subscribe(callback)

When this happens, restart the kernel and you should be able to re-execute. Otherwise, the script runs consistently from the command line.

Kind regards,


0 Kudos