We are pleased to announce a Windows Phone 7 Nirvana client!
If you are familiar with the .NET & Silverlight Nirvana Client API, porting your software to the Windows Phone will be trivial as the Nirvana client API is unchanged.
In this post I’ll take you through a basic “hello world” tutorial.
Chat Application
I thought it would be fun to build a simple chat application, demonstrating how easy it is to perform some bidirectional communications using Nirvana on the phone, desktop or web.
Before we start you will need;
The complete project directory is available for download here.
Step 1. Configure the realm
To start with, lets create channel that will represent our “chat room”.
Channel Name will be “chatroom”.
You should end up with something like this;
I’m also going to give *@* access to the channel & realm, so the phone can send & receive messages to the chat room.
NOTE: you must apply the ACLs at both the realm & channel level.
Step 2. Creating the Phone Project
Open up Visual Studio 2010 and create a new “Silveright for Windows Phone” project. I’m calling it “Chat.Phone”.
Make sure you target “Mango” (Windows Phone OS 7.1).
Add references to;
- System.Observable (WP7 SDK)
- System.Reactive (Rx)
- System.Reactive.Windows.Threading (Rx)
- Nirvana.Phone
- Nirvana.Phone.Reactive
Step 3. Creating the Main Page (XAML)
Here is the xaml for the main page.
<!--LayoutRoot is the root grid where all page content is placed-->
<!-- Message Log -->
<!-- Send Message -->
It should look something like this;
Step 4. Creating the Main Page (C# code behind)
Firstly we need to establish a connection to the realm.
// Initialize realm session
session = new Session("nsp://[REALM IP ADDRESS]");
session.Initialize();
Warning, if you are using the WP7 emulator, you can’t use localhost! This will be the phone’s IP address. You must put the the address of the machine the realm server is running on.
We are going to want to display chat messages in the MessageLog. We can query the chat room topic and append incoming messages to the log like this;
// query realm chatroom topic & update UI when a message is received
var query = from e in session.Topics.CreateConsumer("chatroom").ToObservable()
let username = e.Message.Properties.GetString("Username")
let text = e.Message.Properties.GetString("Text")
select username + ": " + text + "\r\n";
query.ObserveOnDispatcher().Subscribe(x => MessageLog.Text += x);
Finally, we need to wire up some code to send messages to the chat room.
private void Message_TextInput(object sender, TextCompositionEventArgs e)
{
if (!string.IsNullOrWhiteSpace(Message.Text))
{
// send message
var m = new Message();
m.Properties.Set("Username", "Phone User");
m.Properties.Set("Text", Message.Text);
producer.Send(m);
// clear textbox in UI
Message.Text = "";
Focus();
}
}
Here is the C# code in its entirety;
using System;
using System.Windows.Input;
using System.Reactive.Linq;
using MyChannels.Nirvana;
namespace Chat.Phone
{
public partial class MainPage
{
private readonly ISession session;
private readonly IProducer producer;
public MainPage()
{
InitializeComponent();
// Initialize realm session
session = new Session("nsp://10.10.0.2");
session.Initialize();
// create producer used to send messages
producer = session.Topics.CreateProducer("chatroom");
// query realm chatroom topic & update UI when a message is received
var query = from e in session.Topics.CreateConsumer("chatroom").ToObservable()
let username = e.Message.Properties.GetString("Username")
let text = e.Message.Properties.GetString("Text")
select username + ": " + text + "\r\n";
query.ObserveOnDispatcher().Subscribe(x => MessageLog.Text += x);
}
private void Message_TextInput(object sender, TextCompositionEventArgs e)
{
if (!string.IsNullOrWhiteSpace(Message.Text))
{
// send message
var m = new Message();
m.Properties.Set("Username", "Phone User");
m.Properties.Set("Text", Message.Text);
producer.Send(m);
// clear textbox in UI
Message.Text = "";
Focus();
}
}
}
}
If you run the project using the WP7 emulator, you should see something like this;
You can type messages;
But obviously no else else is in the chat room ![]()
Step 5. Windows Application
I’m just going to create a simple console application that we can use to test with. To display messages, I can use the query from the phone application;
// Print out incoming messages
var query = from e in session.Topics.CreateConsumer("chatroom").ToObservable()
let username = e.Message.Properties.GetString("Username")
let text = e.Message.Properties.GetString("Text")
select username + ": " + text + "\r\n";
query.Subscribe(Console.WriteLine);
It is pretty straight forward, here is the code in its entirety.
using System;
using System.Reactive.Linq;
using MyChannels.Nirvana;
namespace Chat.Desktop
{
class Program
{
static void Main()
{
using (var session = new Session("nsp://localhost"))
{
session.Initialize();
Console.WriteLine("Initialized...");
// Print out incoming messages
session.Topics.CreateConsumer("chatroom")
.ToObservable()
.Select(e => e.Message.Properties)
.Subscribe(Console.WriteLine);
// Start a loop, sending messages that the user enters via the console.
var p = session.Topics.CreateProducer("chatroom");
while (true)
{
var m = new Message();
m.Properties.Set("Username", "Desktop User");
m.Properties.Set("Text", Console.ReadLine());
p.Send(m);
Console.WriteLine("Sent");
}
}
}
}
}
If we run both applications at this point, they should be able to communicate with each other.
So there you have it, asynchronous messaging for Windows Phone 7.
Download
You can download the complete project here.