Category Archives: GraphQL

MQTT for Gaming?

MQTT for Multiplayer Games? I am not a Game-Developer and I do not know how they create multiplayer internet games. But I know that MQTT can be used for that. Why?

# IoT turns to IoP

MQTT is used for connecting devices. Think about using MQTT for connecting players. “Internet of Things” (IoT) will turn to „Internet of Players“ (IoP).

# A Player is like a Device?

In a multiplayer game, each player is like a device. When they join the game, it’s like they’re saying “Hello, I’m here!” to everyone else – this is known as a “birth message” in MQTT terms.

# Sharing Information Seamlessly

As players move around in the game, they keep sending updates like their location, healthy state, collected goodies, …. MQTT acts like a messenger, picking up this information and delivering it to everyone else playing. 

# Handling Player Exits

What if a player leaves the game intentionally or unintentionally? MQTT has a smart feature called “last will message.” It’s like a goodbye note that tells other players someone has left the game. This way, everyone stays in the loop.

# Central Management 

A central game management connected to the central MQTT broker, written in any kind of language, could be used to observe and control the game and all the players.

# Why MQTT?

1. **Real-Time Updates**: It’s fast and perfect for real-time games.

2. **Reliable**: Even if a player’s connection is shaky, MQTT makes sure messages get through.

3. **Efficient**: It doesn’t eat up much data, so players won’t lag.

4. **Simple**: It’s not complicated to set up and to use.

# Conclusion

Using MQTT in multiplayer games is like having a super-efficient mailman who ensures everyone knows what’s happening as it happens.

For Unity there is a „Unity for MQTT“ and a “GraphQL for Unity Pro” asset. Easy to use and usable with all the main build targets, including WebGL!

See the game demo I made with MQTT.. I also integrated data from WinCC Unified in the demo, with the GraphQL for Unity Pro Asset.

You can try it out: https://server.rocworks.at/unity/game/ (could be already removed when you try to access it)

3D charts with Unity3D and WinCC Unified

With WinCC Unified V19 we got the feature to read history values of logged tags with GraphQL 👍 I have now added this functionality to the GraphQL for Unity Pro Asset 🤝 

🎥 With that done it is now also very easy to read the history of logged tags in Unity3D. See the video, there we read and visualize the history values of a tag when the application starts. And then the 3D chart gets updated with real-time values coming from a GraphQL tag subscription. 

ℹ️ The GraphQL for Unity Pro Asset has a WinCC Unified interface on top of GraphQL to make the access to WinCC Unified data in Unity even more easier, you do not need to implement the GraphQL queries by your own. 

👉 You can try the demo in the browser! This is possible because the asset als supports WebGL builds and because GraphQL is transported over HTTP&Websockets. Here it is. But it might happen that this link will not work anymore when you read this post.

Note: Use the PWA WebGL Template so that the application adjusts to the size of your browser window. Or adjust the settings in the generated index.html page:

<canvas id="unity-canvas" width=auto height=auto tabindex="-1" style="width: 100%; height: 100%; background: #231F20"></canvas>

💡 Get a copy of WinCC Unified, connect your machines to it and build your Unity3D application with the data from WinCC Unified! 

Example code how to read the last 100 values of a tag.

Connection.LoggedTagValues("PV-Vogler-PC::HMI_Tag_1:LoggingTag_1",
   endTime: DateTime.Now,
   maxNumberOfValues: 100
).ContinueWith((task) => {
   Debug.Log(task.Result.Count); 
}

GraphQL for Unity with WinCC Unified

In this post I will show how data from WinCC Unified V18 can be brought into Unity with the GraphQL for Unity Pro Asset.

First you need WinCC Unified V18 with the GraphQL Server. The GraphQL server comes out of the box with the new version and should be up and running automatically when you download and start your runtime. You should see a process “WCCILgraphQLServer.exe” in your task manager.

In Unity you have to create a project and download the GraphQL for Unity Pro Asset in the Package Manager.

  1. Prefab: Drag and drop the WinCC Unified Prefab from the Prefabs folder into your scene.
  2. Connection: In the properties you have to set your GraphQL Host, optionally the port, the path (typically /graphql), and if you want to have a secured TLS connection (HTTPS, WSS).
  3. Websocket: If you want to subscribe to tag value changes, then you have to open additionally a Websocket connection. Without the Websocket connection you can still read and write tag values, but a subscription to tag value changes is not possible.
  4. Authorization: Set the username and password to connect to WinCC Unified (at the time of writing, this user must have the role HMI Administrator).
  5. Logon: Check the “Logon” checkbox, if you want to start the connection at startup (you can also set this “on-demand” during runtime in your code.

Then you can already start the project in the editor to see if the connection can be established. If everything works fine, then the “Logged On” property turns to checked.

Now you can create your own C# script and read/write/subscribe tag values in an easy way in C# scripting. In that case I have simple added the script as additional component to the WinCC Unified GameObject (Prefab).

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GraphQLWinccUnified;
using Newtonsoft.Json.Linq;

public class WinccUnified3 : MonoBehaviour
{
    private WinccUnified _connection;
    private bool _ready;

    // Start is called before the first frame update
    void Start()
    {
        _connection = GetComponent<WinccUnified>();
    }

    // Update is called once per frame
    async void Update()
    {
        if (!_ready && _connection.IsWebsocketReady()) 
        {
            _ready = true;

            // SubscribeTagValues
            _connection.SubscribeTagValues(
                new string[] { "HMI_String_1" }, 
                data => { Debug.Log(data.GetValue<string>()); });

            // WriteTagValues
            await _connection.WriteTagValues(
                new string[] { "HMI_String_1" },
                new JValue[] { new JValue("Hello World!") },
                new DateTime[] { DateTime.Now });

            // ReadTagValues
            var result = await _connection.ReadTagValues(
                new string[] { "HMI_String_1" });
            Debug.Log(result[0].GetValue<string>());
        }
    }
}

If you deal with with a self-signed certificate, then you must uncheck “Validate Certificate” at the “Graph QL Http” Component. But the Websocket connection in Unity does not yet support self-signed certificates. So, it is better to use an insecure connection. Typically the GraphQL port is 4000, so be sure to set the Port to 4000. And don’t forget to open the firewall for that port on the host where the Unified runtime is running.

AWS AppSync query with Cognito User Pool

If you want to use the Cognito User Pool, then you first have to get somehow the JSON Web Token (jwt). Once you have this token, then you should pass the token in the header within the “Authorization” key.

graphQLHttp.Headers = new List<Header>()
{
      new Header() {Key = “Authorization”, Value = userSessionCache.getIdToken()}
}

Here you will find more information about jwt tokens.

AWS AppSync Real-Time Support for GraphQL for Unity…

The Asset GraphQL for Unity version 1.5.2 works now with AWS AppSync realtime Websocket connections. You can now subscribe to real-time value changes via a GraphQL Websocket connection with AWS AppSync. This was not possible before, because AWS has implemented it’s own specific protocol which is a little bit different to the Apollo Websocket protocol.

Source: https://docs.aws.amazon.com/appsync/latest/devguide/system-overview-and-architecture.html
GraphQL Websocket AWS App Sync Configuration

Sending OPC UA Data with GraphQL to Unity…

In a simple setup I have tested to send 2000 value changes per second from an OPC UA server to Unity with GraphQL, the Open-Source Frankenstein Automation Gateway, and the GraphQL for Unity Asset. And it could go up to 10000 value changes per second…

I had one DotNet OPC UA server with a lot of simulated tags with random data. The .Net OPC UA server is the DotNet reference implementation from the OPCFoundation, which can be found here.

On top of that the Open-Source Frankenstein Automation Gateway for GraphQL was running. It is connected to the OPC UA server. It offers a GraphQL interface to the tags of one or more connected OPC UA servers.

In Unity I had used the GraphQL for OPC UA Asset to easily connect to the Gateway, browse the tags, and subscribe to the value changes of 100 tags.

Each tag was changed by the OPC UA server every 45ms. This ends up in a bit more than 2000 value changes per seconds, which were sent from the OPC UA server to the Unity Application.

Here we see the Application running 3 times on my Laptop with an Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz. In the center you see the amount of incoming value changes per second (red number). Around that number, we see some of the values coming in from OPC UA. At the bottom we see the CPU load of the Frankenstein Automation Gateway (java program) and the OPC UA DotNet Server. Both were running on an old Intel(R) Core(TM) i3-6100U CPU @ 2.30GHz.

It was also possible to increase to load up to 10000 value changes per seconds! Sending 100 values every 10ms from OPC UA to Unity…

GraphQL for Unity and how to set headers in code…

If you use the GraqhQL for Unity asset and if you want to set the headers on HTTP or Websockets in the code, then you must use the the GraphQLHttp or GraphQLWebsockets class. At the base class “GraphQL” there is no “Headers” property!

public GraphQLHttp MyGraphQL;

void Start()
{        
    if (MyGraphQL != null)
    {
        MyGraphQL.Headers = new List<Header>()
        {
            new Header() { Key = "token", Value = "secret"}
        };
        ...

Then you can also execute a GraphQL query in the code.

MyGraphQL.ExecuteQuery("query { test }", null, (message) =>
{
    if (message.Type == MessageType.GQL_DATA)
    {
        Debug.Log("Data: " + message.Result.ToString());
    }
    else
    {
        Debug.Log("Error: " + message.Type);
    }
});
      
   

GraphQL on Unity and Result Event for Data and Errors…

The GraphQL for Unity Asset can be used to execute GraphQL queries in Unity. The result is set on properties of Unity objects and there is also a Unity Event on the GraphQL Query Object where user-defined function of a GameObject can be triggered every time when a queries returns data or an error.

public class Sample1 : MonoBehaviour
{
    public void ResultEvent(GraphQLResult result)
    {
        if (result.Errors.Count==0)
        {
            Debug.Log("Data: " + result.Data.ToString());
        }
        else
        {
            Debug.Log("Error: " + result.Errors.ToString());
        }
    }
}

Create a GameObject with this class as component and then you can drag and drop this GameObject to the Query GameObject and select the “ResultEvent” method. Every time when the query is executed then this function will be called with the result data (or error data).