WebSocket Client

Merged at this point from here:

So after clarifying some additional code that was needed and running from the correct gateway context, it works!

This means we have a way to get data into Ignition from massive Kafka topics by using the lenses SQL api mentioned here:

https://api.lenses.io/branches/4.3/index.html#data-access-query-data-via-sql-get

There were a few more things required, namely encoding the text you send and conversion of the return (here I just put it to string so as to see it in the logs):

queryParams = {
		"token": authToken,
		"stats": 5000,
		"sql": query,
		"live": False
	}
	queryParams = system.util.jsonEncode(queryParams)
	
	class listener(WebSocket.Listener):
		def onOpen(self, websocket):
			logger.info("Event OnOpen")
			websocket.sendText(queryParams, True)
			logger.info("Sent")
			websocket.request(1)
		
		def onText(self, websocket, data, last):
			logger.info("Event OnText")
			logger.info("Response from server: " + str(data))
			websocket.request(1)
		
		def onClose(self, websocket):
			logger.info("Event OnClose")
			
	client = HttpClient.newHttpClient()		
	uri = URI.create(baseUrl.replace("https", "wss") + "ws/v2/sql/execute") 		
	wsObj = client.newWebSocketBuilder().buildAsync(uri,  listener())
	wsObj.get()

Thanks to @pturmel @Kevin.Herron and @PGriffith for your help.

Nick

4 Likes