Add WebSocket connection to StreamService
AlexArcPy opened this issue · 6 comments
ArcRest
3.5.6
Enhancement
Shall we add support for the StreamService that comes with GeoEvent Extension? I've just added support StreamService now in the main repo, so we can get static properties. It would be really cool if one could connect to the service via the WebSocket and listen to events in Python. The REST page of Subscribe.
To connect with web sockets in Python, I used the websocket-client 0.37.0 package. Seems to be easy to use and stable. Below is a simple snippet for reading events from StreamService.
What's the best way to build in the websocket client so users don't need to install it themselves? Another thing is we are going to need to establish connection with ws://
, so the current code will fail to run at the point of url type verification (it's not http/https).
I tested on a local simple StreamService and the code below worked fine.
Repo Steps or Enhancement details
from arcrest.ags.server import Server
from arcrest import AGSTokenSecurityHandler
from websocket import create_connection
ags_securityHandler = AGSTokenSecurityHandler(username='user',
password='user',
token_url=r'https://machine:6443/arcgis/tokens/')
url = r'https://machine:6443/arcgis/rest/services'
ags_service_obj = Server(url,ags_securityHandler)
ss = [s for s in ags_service_obj.services if
s.url == r'https://machine:6443/arcgis/rest/services/Service_DEMO/StreamServer'][0]
ws = create_connection("wss://machine:6143/arcgis/ws/services/Service_DEMO/StreamServer/subscribe?token={0}".format(ags_securityHandler.token))
ws.next()
>>> gives the next event in JSON
@AlexArcPy I have not really looked at this, but do you think you could leverage the socket library?
@achapkowski Andrew, let me check if socket module is capable of establishing web socket connection. Doesn't look as easy to work with as using websocket-client package.
@achapkowski I have browsed around and it doesn't seem as as builtin module socket
supports establishing connection via wss
which is required to connect to a stream service. Do you think it's a bad idea to add some code that will depend on a 3rd party package? We could probably let users know that they need to install another package themselves?
@AlexArcPy I am not opposed to using 3rd party modules as long as the license allows for there use in this type of project.
@AlexArcPy use the 3rd party module.
@achapkowski , thanks for getting back on this. I just didn't have time to respond to you; so this issue is paused on my site so far... sorry for that!