# WebSocket Client

**URL:** <https://forum.inductiveautomation.com/t/websocket-client/44447>\
**Category:** Ignition\
**Tags:** ignition81, vision, scripting\
**Created:** [March 14, 2021, 8:48pm UTC](https://forum.inductiveautomation.com/t/websocket-client/44447 "2021-03-14T20:48:08Z")\
**Posts on this page:** 1\
**Showing post:** 21

<div class="post-metadata">

**Author:** ![nicholas.robinson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nicholas.robinson/32/22188_2.png) [@nicholas.robinson](https://forum.inductiveautomation.com/u/nicholas.robinson)\
**Post date:** [February 21, 2022, 7:01pm UTC](https://forum.inductiveautomation.com/t/websocket-client/44447/21 "2022-02-21T19:01:32Z")

</div>

Merged at this point from here:

> [@API Endpoint Certs](https://forum.inductiveautomation.com/t/api-endpoint-certs/55461/9):
>
> I am going to merge this thread into this: [https://forum.inductiveautomation.com/t/websocket-client/44447/20](https://forum.inductiveautomation.com/t/websocket-client/44447/20) The summary here is that by moving this to a gateway script (as opposed to running it all from the script console) overcomes the error named: unable to find valid certification path to requested target Thanks, Nick

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/6/b6ec3e10fc1ac733a36427b1e4ed7456140024ff.png)

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](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):

```auto
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 @paul-griffith for your help.

Nick

---

_[View the full topic](https://forum.inductiveautomation.com/t/websocket-client/44447)._
