Push from Uno to Javascript

Hi!

Is there a way to push/call a method in javascript from Uno? So for example if Uno had a listener that was listening for messages, and when something arrives the listener in Uno will let javascript know that something has arrived.

Thanks!

Eddie

Hi!

Here is how you can implement an event in Uno which calls back to your JavaScript (the docs will be updated to include this soon):

using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;

namespace JSUno
{
    public class Chat : NativeModule
    {
        NativeEvent _nativeEvent;

        public Chat()
        {
            AddMember(new NativeFunction("send", (NativeCallback)SendMessage));
            _nativeEvent = new NativeEvent("onMessageReceived");
            AddMember(_nativeEvent);
        }

        object[] SendMessage(Fuse.Scripting.Context context, object[] args)
        {
            var arg = args[0] as string;
            if(arg != null)
                _nativeEvent.RaiseAsync(arg);
            else
                _nativeEvent.RaiseAsync("----");

            return null;
        }
    }
}

var chat = require("chat")
function send() {
    chat.send(new Date().toString());
}

chat.onMessageReceived = function(message) {
    console.log("onMessageReceived " + message);
};

Add this to your UX to make the chat module available in JS: <JSUno.Chat ux:Global="chat" />

thanks! that worked like a charm :slight_smile: