Expand my Community achievements bar.

SOLVED

SimpleChat Serverside Logic - using recipientDisplayName

Avatar

Level 2

Hey guys,

I'm working on a application within AFCS that requires all users to be connected to the same room (I have a general chat room), but then also allows users to send private messages to each other.  However, as you likely know, when using a shared model, all messages get broadcast to everyone within the room, which is not the experience that I want.

I knew that SimpleChat had very similar functionality, so I dug into it a bit.

I'm going to assume that you guys ran into the same problem that I'm having because you still have commented out code in SimpleChatModel, lines 742 - 750:

/*
if ( (msgDesc.recipient is String && msgDesc.recipient != _userManager.myUserID) //it's a private but not for me
     || (msgDesc.recipient is int && msgDesc.recipient > _userManager.myUserRole) )     //it's for a role higher than mine
{
     //ignore this message
     //TODO: Nigel: I shouldn't be getting this at all!
     return;     
}
*/

After poking around for a bit, I learned that in the default_SimpleChat application on the server, if I want to send messages privately from one client to another I can set recipientDisplayName property on the ChatMessageDescriptor.

I'm guessing the solution you guys came up with is occurring on the server-side, so I ran a quick test in my console and verified that my history collection does not get updated when I send a private message.

So... the million dollar question... Is there any api or way for me to accomplish this same sort of private messaging within my own application?  Is the answer just sitting there right in front of me?

P.S. -- I found it quite odd that you guys are not encoding the recipient property in your createValueObject method in ChatMessageDescriptor.as, but you are reading it.  I don't really understand why you guys chose to use recipientDisplayName instead of the recipient's id, was there a reason you chose to use the display name?

/**
* @private
*/
public function readValueObject(p_valueObject:Object):void
{
     displayName = p_valueObject.displayName;
     msg = p_valueObject.msg;
     color = p_valueObject.color;
     recipient = p_valueObject.recipient;
     role = p_valueObject.role;
     recipientDisplayName = p_valueObject.recipientDisplayName;
}

/**
* @private
*/
public function createValueObject():Object
{
     var res:Object = new Object();
     res.displayName = displayName;
     res.msg = msg;
     res.color = color;
     res.role = role;
     res.recipientDisplayName = recipientDisplayName;
     return res;
}

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Hi,

If you want to do private messaging in any of your model, when you pass a message i.e. create the MessageItem, you need to assign it to a recipient id. If there is a recipient id, which is nothing but the user id of the user you want to send, then the message will be sent to that user. By default, there is no recipient id in a message item, so its broadcasted to everyone.

You will see that SimpleChatModel.as creates the recipient id if the ChatMessageDescriptor has a recipient in the sendMessage function. And the SimpleChat.as creates the recipient in the ChatMessageDescriptor in case , you select someone specific to send the chat to inside the function sendNewMessage

Hope this helps.

Thanks

Hironmay Basu

View solution in original post

2 Replies

Avatar

Correct answer by
Former Community Member

Hi,

If you want to do private messaging in any of your model, when you pass a message i.e. create the MessageItem, you need to assign it to a recipient id. If there is a recipient id, which is nothing but the user id of the user you want to send, then the message will be sent to that user. By default, there is no recipient id in a message item, so its broadcasted to everyone.

You will see that SimpleChatModel.as creates the recipient id if the ChatMessageDescriptor has a recipient in the sendMessage function. And the SimpleChat.as creates the recipient in the ChatMessageDescriptor in case , you select someone specific to send the chat to inside the function sendNewMessage

Hope this helps.

Thanks

Hironmay Basu

Avatar

Level 2

Ah I see it:

For nodes where <code>allowPrivateMessages</code> has been set to true, this field allows messages to be received by only *one* recipient. Note that for cases where groups of people are recipients, you should use specialized nodes where those recipients have been promoted with the ability to subscribe. We do want to avoid the one-to-one private message case devolving to "one node per user," so recipientID allows this in a much simpler manner.

public var recipientID:String;

I didn't think to lookup MessageItem because ChatMessageDescriptor doesn't extend from it.

Thanks again! I love this framework!

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----