Create persistent websocket in Ignition

Hello,
I want to open a websocket and make it open so the I can get the data back from server.

I really need some best practices on how to do that.

Hi nader.chinichian,

Do you want Ignition to act as a socket server?
This wouldn't be recommended as you'd have to run a script eternally.

If that's not what you are seeking to accomplish, please clarify what you are looking to accomplish so that we can offer you a direct solution.

Yes, I want Ignition to be a WebSocket server, something like a web dev module but I don't want to create a module for that.
So I wonder if is it possible with Jython and run that script in gateway scope by something like system.util.invokeAsynchronous() and send receive msg to the related perspective session.
I did it by web dev simple REST API endpoints but as per request it should open and close connection it is performant as WebSocket.
What I really want here is call to class JAVA websocket class directly in Jython instead of Python native Lib.

I wonder if this might be an application where you could leverage something like MQTT Engine with a custom namespace, tied to a broker that supports websockets (i.e. mosquitto, but I'm pretty sure many of the others out there also support this). This way you could interface with the broker (from whatever client application you're thinking about here) via websockets and have Ignition tied to the broker via MQTT. Just brainstorming on some possibilities here. :thinking:

2 Likes

Ignition uses the jetty libraries to run its webserver and provide its HTTP(s) clients. That includes websocket support.

Start with Jetty's Programming Guide.

However, you will need to adjust for the version of Jetty provided by Ignition, which is v9.4. So use these javadocs instead of the v11 javadocs.

Edit: Hmmm. It appears that Ignition does not package Jetty's websocket client library. You may need to use Java 11's native support:

https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/package-summary.html

Good luck. And report back, please. (:

Edit2: Paul corrected me. Jetty's websocket is bundled after all.

2 Likes

Naturally, I am now curious how Perspective upgrades its browser connection to a websocket. @PGriffith ?

1 Like

The platform bundles org.eclipse.jetty.websocket:websocket-servlet. As far as I know, as of 8.0.0, for use in Perspective.

Since the gateway is acting as the server, that's all we need there.
The browser does the websocket client handshaking.

For GAN, it looks like we also use Jetty's WebsocketClient (org.eclipse.jetty.websocket.client.WebSocketClient).

1 Like

It is actually running JavaScript inside of an
Iframe embedded in perspective
This way people can send and get data to and from perspective and make their component with js.
Mqtt is costly and resource hungry solution for that.

I think, if you want it to operate on the same web address as Ignition, you will need a module to register a servlet. Jython won't be enough.

Jython with a long-lived thread might be able to open another port (I think), but Content-Security-Policy will be trouble.