# Ignition as WebSocket client

**URL:** <https://forum.inductiveautomation.com/t/ignition-as-websocket-client/112935>\
**Category:** Ignition\
**Tags:** ignition83\
**Created:** [December 21, 2025, 8:43am UTC](https://forum.inductiveautomation.com/t/ignition-as-websocket-client/112935 "2025-12-21T08:43:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoWay](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoWay](https://forum.inductiveautomation.com/u/NoWay)\
**Post date:** [December 21, 2025, 8:43am UTC](https://forum.inductiveautomation.com/t/ignition-as-websocket-client/112935/1 "2025-12-21T08:43:30Z")

</div>

Hi,

I would like to set up ignition so listen to a WebSocket server as a client. What is the best practices to do so?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 21, 2025, 2:39pm UTC](https://forum.inductiveautomation.com/t/ignition-as-websocket-client/112935/2 "2025-12-21T14:39:13Z")

</div>

Possible with jython scripts.

> [@WebSocket Client](https://forum.inductiveautomation.com/t/websocket-client/44447/21):
>
> 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](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…

---

<div class="post-metadata">

**Author:** ![NoWay](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoWay](https://forum.inductiveautomation.com/u/NoWay)\
**Post date:** [December 22, 2025, 9:39am UTC](https://forum.inductiveautomation.com/t/ignition-as-websocket-client/112935/4 "2025-12-22T09:39:57Z")

</div>

So, I managed to get this script working. At the moment just running for Script Console.

What is the best way to start the script on gateway startup and maintain the connection so it reconnect if it looses connection?

```python
from java.net.http import HttpClient
from java.net.http import WebSocket
from java.net import URI
import sys

	# Define a custom listener class that implements WebSocket.Listener
class WebSocketListener(WebSocket.Listener):
    def onOpen(self, websocket):
    	print("WebSocket Connection Opened")
        # Request more messages, or send the first message here
        websocket.sendText("Hello from Ignition", True)
        websocket.request(1) # Request the next message

    def onText(self, websocket, data, last):
        print("Received message: " + str([data]))
        # Process the received data (e.g., write to a tag)
        system.tag.writeBlocking(["[default]My/Data/Tag"], str([data]))
        websocket.request(1) # Request the next message

    def onError(self, websocket, error):
        print("Error: " + str(error))
        # Handle the error
        error.printStackTrace()

    def onClose(self, websocket, statusCode, reason):
        print("Connection closed: " + reason + " Status Code: " + str(statusCode))

# Create an HttpClient instance and connect
try:
    client = HttpClient.newHttpClient()
    listener_instance = WebSocketListener()
    client.newWebSocketBuilder().buildAsync(URI.create("ws://192.168.2.21:1880/ws1"), listener_instance)
    print("Connecting to WebSocket server...")
except Exception as e:
    print("Failed to establish WebSocket connection: " + str(e))
 

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 22, 2025, 1:35pm UTC](https://forum.inductiveautomation.com/t/ignition-as-websocket-client/112935/5 "2025-12-22T13:35:20Z")

</div>

You'll need to make a project library script out of this, and study the traps related to long-lived asynchronous threads:

Some reading for you: [long lived async thread](https://forum.inductiveautomation.com/search?q=long%20lived%20async%20threads%20after%253A2021-01-01%20order%253Alatest)
