Question

Freedom UI get message from process

Hi Team, Someone knows if this functionality is possible from the Freedom UI page?

 

init: function() {
    this.callParent(arguments);
    // register our function to receive messages
    Terrasoft.ServerChannel.on(Terrasoft.EventName.ON_MESSAGE, this.onServerMessageReceived, this);
},
 
onServerMessageReceived: function(scope, message) {
    var sender = message && message.Header.Sender;
    // make sure the message received is the one you sent
    if (sender === "SomeMessageId") {
        // if you sent some data with the message you can get it from the message Body
        var someMessageText = message.Body;
        // do something here as needed
    }
},
 
destroy: function() {
    // unregister so we no longer get messages
    Terrasoft.ServerChannel.un(Terrasoft.EventName.ON_MESSAGE, this.onServerMessageReceived, this);
    this.callParent(arguments);
}

 

Like 0

Like

2 comments

I'm sure there's a better way than this (I would love to find out there's built in support for listening for server messages in Freedom UI pages). But for now, this works - if anyone knows the *right* way to do this in a Freedom UI page, I'd love to see it (because this way doesn't seem right, although it works).

(1) First, add an attribute to the viewModelConfig, like this:

viewModelConfig: /**SCHEMA_VIEW_MODEL_CONFIG*/{
	"attributes": {
		"MySocketFunction": {}
	}
}/**SCHEMA_VIEW_MODEL_CONFIG*/,

(2) Now add a handler for the "crt.HandleViewModelInitRequest". In it, you'll create a function and place in that attribute, then wire it up for receiving server messages:

{
	request: "crt.HandleViewModelInitRequest",
	handler: async (request, next) => {
		request.$context.MySocketFunction = (context, message) => {
			if (message.Header.Sender === "SomeMessageId") {
				Terrasoft.showInformation("Got the message - do something");
			}
		}
		Terrasoft.ServerChannel.on(Terrasoft.EventName.ON_MESSAGE, (await request.$context.MySocketFunction), request.$context);
		return next.handle(request);
	}
}

(3) Now, add the destroy in the "crt.HandleViewModelDestroyRequest" request

{
	request: "crt.HandleViewModelDestroyRequest",
	handler: async (request, next) => {
		Terrasoft.ServerChannel.un(Terrasoft.EventName.ON_MESSAGE, (await request.$context.MySocketFunction), request.$context);
		return next.handle(request);
	}
}

I anxiously await learning a better way to do this, but for now this works. Also, you can avoid using the attribute and putting the function in it if you use a module instead, then just wire up a function defined in the module. But without a module, you'll have to use the attribute since you can't add function on a Freedom UI page (which is aggravating).

It would be amazing to find out there's a request you can add a handler for to receive server messages, but I've not found anything like that yet.

Ryan

Ryan Farley,

Thank  you as always for the support. 

Same here, looking for documentation or ways to replicate the same logic we have in the current UI. 

It will be very fun times for adoption smiley

Show all comments