[project] SimpleMessaging: cascading message delivery

This project simplifies component to component interaction through system.util.sendMessage. This approach is used in Unity3D and is helpful.

How to Send Message

    shared.simplemessaging.client.send("TOAST", { "message": "hello world", "severity": 7<a class="attachment" href="//cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/business4/uploads/inductiveautomation/original/2X/1/1b45e1133f2823d8916b7b02f76ffab37d6712d7.proj">SimpleMessaging_2017-10-04_0732_partial.proj</a> (2.9 KB)
 })

The first argument is the message type. The receiving method name will include the message type in its name.
The second argument is a message as a dictionary. The contents are up to your implementation but it must be a dictionary.

How to Receive Message
Your project needs a Client Message Handler named ‘OnSimpleMessage’. The script of the handler is:

    shared.simplemessaging.client.onClientMessageReceived(payload)

In order for someone to receive the sent message we need to create a custom method on any one of our components in the component hierarchy. The format of the custom method name is ‘OnMessage_{message-type}’. For the above example, the method name will be ‘OnMessage_TOAST’ with a single argument of **kwargs. Inside the receiving script you can access your message like so:

def OnMessage_TOAST(**kwargs):
    if kwargs is not None:
		for key, value in kwargs.iteritems():
			print "%s == %s" %(key,value)    

    isRemoteSource = False if system.util.getClientId()==kwargs["payload"]["source"] else True
	message = kwargs["message"]["content"] if "content" in kwargs["message"] else ""
	severity = kwargs["message"]["severity"] if "severity" in kwargs["message"] else 7
	self.AddToast(message,severity,isRemoteSource)

If you want a component to receive all messages sent through the system, you can add a method named ‘OnMessage_ANY’. This method will catch any messages that do not have a message type specific receiver.

SimpleMessaging_2017-10-04_0732_partial.proj (2.9 KB)

1 Like