New feature "Messages" and messages-mixer


#1

Today we added a handy little feature to allow scripts to send messages to other nodes on a domain. The basic system works as follows:

Messages.subscribe(channel); // subscribes to a channel "channel"
Messages.unsubscribe(channel); // unsubscribes from channel "channel"
Messages.sendMessage(channel, message); // sends "message" to channel "channel"

// connect to the messageReceived signal
Messages.messageReceived.connect(function(channel, message,sender) {
    print("got message:"+message+" on channel:" + channel + " from sender:"+sender);
}); 

There are a couple of example scripts as well that show this usage.

https://github.com/highfidelity/hifi/blob/master/examples/example/messagesExample.js
https://github.com/highfidelity/hifi/blob/master/examples/example/messagesReceiverExample.js

You can use these new script methods in AC script, in entity scripts, or in regular local file scripts.

If you are running the latest stack manager, you’ll get the messages-mixer automatically. If you run your own deployment of assignment-clients, you will want to make sure to run a type “4” assignment client to get this feature.

Enjoy!


#2

On behalf of those of us struggling with WebSockets…
Is this helpful? Jean-Luc what say you?

Well there you have it. We are pleased… :smile:


#3

Just what I needed when I needed it!

I’m glad channel can be a string, unlike “other” VWs that only use numbers for their “channel”.


#4

I have one suggestion. Maybe a script shouldn’t receive its own messages, that way you can use the event as a trigger to send a message back (and it won’t cause an endless loop). I suppose you could check the sender though. Maybe sending itself a message could be handy somehow I just can’t think of a reason ATM.


#5

I attached the script to a box and got this (and I have a message-mixer AC running):

[WARNING] [UncaughtException] ReferenceError: Can't find variable: Messages in (EntityID:{eeb3eb69-8b91-4820-b0c5-b0b34abc474f}

In fact… I ran the currentAPI script on a box and this is what I got
script : print()<< “\n======= JS API list =======\n========= API END =========\n”

I feel like I am missing something really obvious.


#6

Looks like the Stackmanager didnt update (since Messages variable is undefined)

Check the versions


#7

You will only receive messages for channels you subscribe to. So if you don’t want any messages, don’t subscribe.
Messages will also come with the senderID, so you can filter out messages that you sent.


#8

Make sure you’re running the latest Interface client.


#9

Today I’ve noticed that my Interface stats show 5 servers now, most of the time. But on both domains every once in awhile it drops to 4 and then back to 5. The timing of the server count seems to match the log entries:

[11/18 09:34:39] [DEBUG] [4616] [messages-mixer] Activating local socket for network peer with ID “26881403-b1a2-4578-9f84-aae8056d0568”
[11/18 09:34:54] [DEBUG] [4616] [messages-mixer] Killed “Agent” (I) {26881403-b1a2-4578-9f84-aae8056d0568} 73.170.132.228:56503 / 192.168.0.196:56503
[11/18 09:34:55] [DEBUG] [4616] [messages-mixer] Added “Agent” (I) {26881403-b1a2-4578-9f84-aae8056d0568} 73.170.132.228:56503 / 192.168.0.196:56503
[11/18 09:34:55] [DEBUG] [4616] [messages-mixer] Activating local socket for network peer with ID “26881403-b1a2-4578-9f84-aae8056d0568”
[11/18 09:35:09] [DEBUG] [4616] [messages-mixer] Killed “Agent” (I) {26881403-b1a2-4578-9f84-aae8056d0568} 73.170.132.228:56503 / 192.168.0.196:56503
[11/18 09:35:10] [DEBUG] [4616] [messages-mixer] Added “Agent” (I) {26881403-b1a2-4578-9f84-aae8056d0568} 73.170.132.228:56503 / 192.168.0.196:56503
[11/18 09:35:10] [DEBUG] [4616] [messages-mixer] Activating local socket for network peer with ID “26881403-b1a2-4578-9f84-aae8056d0568”


#10

This is a benign but annoying issue… there’s a PR coming shortly that will fix this to quiet the logs.


#11

What I am saying is if you wanted to use the channel as a trigger to send something back you will get in an endless loop, but yes you could filter it out.

I just 100% recompiled AC and DS and updated interface to 3593 and same deal. Running on Ubuntu Linux Server and Windows 10 Interface.

[WARNING] [UncaughtException] ReferenceError: Can't find variable: Messages in (EntityID:{57ed875e-02a1-43fe-a801-dbf0d3c99f2c}

If I run the script as an anonymous function like so:

(function(){
		var channel = "test";

		Messages.subscribe(channel); 
		Messages.messageReceived.connect(function(channel, message,sender)
		{
			Messages.sendMessage(channel, "Hello, world!"); 
		});
});

It does nothing at all when I send to the channel. If I comment out the anonymous function I get the error.

The script is attached to a simple red box created with edit.js in the script url field (so it’s an entity script, not AC).


#12

that’s the design. sender ID included so you can filter out your own messages if you want to.


#13

I will test with entity scripts and see what I get.


#14

@Cracker_Hax - it looks like you’re attempting to do an entity script. The following is an example of an entity script that subscribes to the example channel and prints out messages sent to that channel. I have tested this with the latest release build 3594 and it works as expected.

https://github.com/ZappoMan/hifi/blob/messaging/examples/entityScripts/messagesReceiverEntityExample.js


#15

Cool thanks for the example, it worked. I haven’t messed with entity scripts yet. I thought they would work with just a self-invoking function. Apparently they don’t.


#16

@ZappoMan The senderID value appears to be that of an internal senderNode rather than the UUID of the avatar that sent the message. I.e., senderID !== MyAvatar.sessionUUID when receive own messages.


#17

Ok, I’ll look into that.


#18

This bug has been fixed in a recent build. You will need to upgrade your Stack Manager and Interface client to get the new correctly functioning code.


#19

Thanks @ZappoMan for the fix.

Next up: getting the user name for the sender from the senderID …

I can see nowhere in the JavaScript API where you can retrieve the user name for a UUID.

If the avatar is not your own you can use AvatarManager.getAvatar(senderID) to get a bunch of data that includes the sender’s displayName but not their user name. Perhaps the user name could be added to the data returned?


#20

We have not added this to date for security reasons. I will ask the team again if we feel that is really necessary.