How to get the "is typing..." messages through UCMA

If you''re writing a UCMA application that handles instant messaging, you may at some point want to replicate the effect that users of the Lync client get where a message shows up notifying you when the other person is typing. This makes it a bit easier for people to carry on a conversation over instant messaging without constantly interrupting one another. Triggering and receiving this notification is actually quite easy in UCMA, but the way to do it is not immediately obvious. I''ll start with how to receive typing notifications: that is, how your UCMA application can find out when someone on the other end of an IM conversation is typing. You can do this by hooking up an event handler to the RemoteComposingStateChanged event on an InstantMessagingFlow object, which you can find on the Flow property of InstantMessagingCall. The "composing state" is just a fancy term for whether the remote party is typing or not, and there are two possible composing states: composing, and idle. Here''s what the code looks like for subscribing to the event:

[csharp] imCall.Flow.RemoteComposingStateChanged += new EventHandler<ComposingStateChangedEventArgs> (OnRemoteComposingStateChanged); [/csharp]

In your event handler, you can then check what the new state is and respond accordingly, as shown in the following code:

[csharp] void OnRemoteComposingStateChanged(object sender, ComposingStateChangedEventArgs e) { if (e.ComposingState == ComposingState.Composing) { ShowTypingIcon(); } else { HideTypingIcon(); } } [/csharp]

Easy enough, right? Now, what if you want to have your application send that typing notification to a remote party? To do this,you can use the LocalComposingState property on InstantMessagingFlow:

[csharp] imCall.Flow.LocalComposingState = ComposingState.Composing; [/csharp]

Once you''ve set the LocalComposingState property to ComposingState.Composing,UCMA will send typing notifications to the remote party every few seconds, so that the typing message continues to show up, until one of three things happens:

  • You send an IM using InstantMessagingFlow.BeginSendInstantMessage
  • You explicitly set LocalComposingState to ComposingState.Idle
  • The composing state timeout elapses

You can set the composing state timeout using the ComposingStateTimeout property on InstantMessagingFlow. After this number of seconds go by, the composing state will automatically revert back to Idle, unless you''ve set the ComposingState property again during that time.

[csharp] // Typing notifications will stop being sent // after 30 seconds. imFlow.ComposingTimeoutValue = 30; [/csharp]

That''s more or less all there is to it. If you have any questions about how this works, don''t hesitate to write a comment or email me.