Virtual Architectures
Virtual Architectures

Reputation: 35

How to emit events from Node-Red to a Unity client with node-red-contrib-socketio

I'm trying to use node-red-contrib-socketio package to emit a 'weather' event from Node-Red to a client based on input from Weather Underground.

Emit Weather Event from Node-Red

I'm using the following code in a node-red function to process the input from WeatherUnderground and set the event:

Setting 'weather' event

weather = msg.payload.weather;
msg.payload = {weather: weather};
msg.socketIOEvent = 'weather';
RED.util.setMessageProperty(msg, "socketIOEmit", "emit", true);
return msg;

Is this the correct way to set and emit the weather event?

For reference:

I've bound the SocketIO Out node to Node_Red (so presumably port 1880 on local host). I'm using the Unity game engine as the client to receive the event with the Socket.IO library from the asset store: https://assetstore.unity.com/packages/tools/network/socket-io-for-unity-21721

Unity is listening for the weather event on the following URL:

ws://127.0.0.1:1880/socket.io/?EIO=4&transport=websocket

Currently Unity seems to be registering the connection but not the emitted weather event.

My test C# script for handling the events in Unity is as follows:

using UnityEngine;
using SocketIO;

public class NodeNetwork : MonoBehaviour
{

    //Reference socket component
    static SocketIOComponent socket;

    void Start()
    {
        //Initialise reference to socket component
        socket = GetComponent<SocketIOComponent>();
        //Register callbacks for network events
        socket.On("open", OnConnected);
        socket.On("weather", OnWeather);
    }

    //Create a callback for the connection
    void OnConnected(SocketIOEvent e)
    {
        Debug.Log("Connected");
        //Emit a move call back to the server 
        socket.Emit("client connected");
    }

    //Create a callback for receipt of weather events
    void OnWeather(SocketIOEvent e)
    {
        Debug.Log("New weather event received" + e.data);
        socket.Emit("weather received");
    }

}

Any advice would be appreciated.

Upvotes: 1

Views: 3439

Answers (2)

Virtual Architectures
Virtual Architectures

Reputation: 35

After further research and testing I got this working. The Node-Red flow was fine but some sort of configuration issue prevented it working. Reinstalling Node.js and Node-Red resolved the issue.

Using msg.socketIOEmit = "emit" as advised by @hardillb works.

I tested several SocketIO solutions for Unity and ended using SocketIO for Native and WebGL builds by DASPETE which is a $10 paid asset.

In order to deserialise the JSON I used SaladLab's JSONNetLite Unity package which is a fork of NewtonSoft.JSon.NET.

To successfully use the package in a Unity WebGL build you need to add a link.xml file to your assets folder. This adds exceptions to the default Unity bytecode stripping which removes unused code from DLL's like the Newtonsoft.Json package.

I hope that helps if you have the same issues.

Upvotes: 0

Antry
Antry

Reputation: 458

From the code you provided and the owner of the asset it seems to be the DEPRECATED SocketIO Unity port, I'd advise you to look into socket.io-unity (which is free from github and 10$ from the asset store) which is a revised version of Quobject's SocketIoClientDotNet to work with Unity3D

I know this is not a concrete answer (apart from switching library) to fixing your problem, but the owner of your package has said himself over 2 years ago now that he had stopped development of it. Hence why I think it would be interesting to switch library as primary option, I actually think you did things right, and it just doesn't work. I'm currently at work and can't test this out, sorry. I hope I was able to help in some way.

Upvotes: 1

Related Questions