Build p2p chat application with WPF and ASP.NET Web API

Recently I’ve been really enjoying myself with ASP.NET Web API. It is a tremendous beast, and with it’s self-hosting capabilities, it’s suitable as an HTTP-channeled-communication not only for ASP.NET websites, but also for any other .NET applications. In this post, I’m gonna try to show you the outcome of my weekend mash up – a peer-to-peer WPF chat application (clients connect directly to each other), fueled by ASP.NET Web API.

More after the jump.

What are we gonna do

So, as mentioned, we’ll try to build a chat application with WPF. It’s gonna be targeted for .NET 4.5 (possible to refactor it into .NET 4 easily as well), make use of of ASP.NET Web Api self-host, and allow us to communicate smoothly over the network through HTTP. Heck, we might even have to ditch Live and Skype by the end of this article.

With ASP.NET Web Api self-host, we can turn any C# product into a web server. What this means, is that each of our chat apps (or “clients”) will essentially be web servers, listening to incoming HTTP requests on a certain port. The idea is simple – if we have two chat “clients” out there in the network, and each of them is a web server with IP address and a port to which they listen – they can easily communicate between each other in a P2P way. We are going to use POST requests as a way to transport messages between the clients.

Getting started

We’ll start off with a normal WPF project, target it to .NET 4.5 and get the necessary self host packagaes from Nuget. To do that, go to Nuget and grab AspNetWebApi.SelfHost. If you do it from VS, it will automaticall get all dependencies as well. If not, you need the Core as well.

XAML grid and all that

Let’s start with the XAML UI. We’ll need the following pieces in our UI:
– a textbox to display the chat conversation. It’s gonna keep scrolling down if text doesn’t fit anymore
– a textbox to enter the message to send
– a textbox to set username
– a button to send the message
These are all the core functionalities.

Additionally, a few configuration options will be needed in the UI as well:
– a textbox to choose the local port to listen to (for incoming messages)
– a textbox to enter the IP of the chat partner (for outgoing messages)
– a button to start the chat with the provided settings (for, well, clicking)

Below is out wonderful UI in all its glory. It’s far from being a home run app GUI-wise, but for the purpose of this exercise it will have to suffice πŸ™‚

Let’s have a look at the XAML code required for setting this up.

Nothing very surprising here. We have 4 rows:
– 1st will act as a chat text area,
– 2nd is the input row for new messages,
– then labels
– and finally the settings row.

Notice the funky margins – I was too lazy and just arranged the elements on the designer manually πŸ™‚

I have bound 3 events:
– KeyDown event for the chat input textbox – to detect Enter press and send the message
– click on the send button – to do the same
– click on the start chat button, which is to get the chat application up and running in the first place.

Organizing the code because GUI and Business shouldnt clash

I separated the application into three main namespaces: Filip.ChatGUI, Filip.ChatBusiness and Filip.ChatModels and their purpose is rather self explanatory. In the GUI part we’ll merely deal with setting and populating UI elements, while the whole communciation-between-clients extravaganza part will happen in the Business namespace. Models part will contain our, well, models: for the Message object and for MessageReceiver.

Let’s have a look at the GUI code:

First of all, we’ll need a private instance of the ChatProxy class. We’ll go into details of that class later, so let’s leave it aside for now.

This ChatProxy property, is going to be the heart and soul of our application. It’s going to be instantiated by the user using a “start chat” button click (as long as proper settings are passed fro mthe GUI).

We will send messages using the following public method of MainWindow (which in turn calls a public method of ChatProxy):

In fact this method, and Status property, are the only public members that ChatProxy exposes to the GUI layer – after all, the GUI doesnt need ChatProxy for anything more (in the future you might want to add maybe methods to start/stop chat server and so on…).

Anyway, as you see, the message itself is going to be wrapped in the Message object. More on that class soon. In the
meantime, let’s think a little about how is this above method going to be called? Well, we need 3 aforementioned event handlers.

1. Handler for text input:

This is waiting for the Enter key press, and when it’s pressed and the ChatProxy is instantiated, and username and message text are provided, it sends the message (in the form of the Message class) to the Business layer.

2. Handler for clicking on send button:

This is very similar as the previous one.

As you may have noticed, the handlers make use of two other methods – ShowStatus, used for displaying MessageBox upon erros, and ShowMessage() which will be used to display incoming messages from our chat partner.

Let’s have a look:

3. Clicking on start chat button:

This button instantiates the ChatProxy and makes our chat ready to go.

In both cases the methods use Dispatcher.Invoke to update the UI. The reason for that is that we are going to pass them as delegates to ChatProxy object and invoke from there, so they are going to be called from a different thread.

Let’s come back for a moment to the ChatProxy constructor, as instantiating of it is arguably the most interesting thing happening in the GUI layer. The object is constructed accordingly:

The constructor looks like this:

where, the first two arguments are delegates, which we will use for updating the GUI from the Business layer. The other two are going to be used to start our local self-host server (myport) and to tell HttpClient who to send the messages to (partneraddress).

Modelling, anyone?

OK, so seems we have covered the entire GUI layer, and we can now smoothly transition to looking at the models.
Let’s a have quick peek at the message class.

The Message class has three simple properties – username, text and sent (DateTime). Additionally, it contains a property called “SerializedMessage”, which we’ll use to transmit the message over a POST request to the chat partner. The class has two constructors – which are two sides of the same coin, so to speak.

The first constructor is used for receiving messages (that’s why it takes the MessageReceiver instance as an argument). The second consturctor is used for building up the message that is going to be sent.

MessageReceiver class is very simple.

We’ll use it in the incoming POST events to pull out data from the HttpRequest. This is a neat technique that Rick Strahl generously showed on his blog.

Business is bussiness, so where does all that heavy lifting happen?

GUI – check. Models – check. Time for Business.

Since we are building the application around ASP.NET Web API, we couldn’t do without a class inheriting from ApiController. Remember, we are only interested in handling POST events – which in our app will represent incoming messages.

Our ChatController’s Post method takes in the MessageReceiver instance and transforms into Message object. Additionally we need to do one more thing. We’d like the controller to notify the ChatProxy class that a message has arrived and it might be useful to display it in the GUI. How about using a static event for that?

So after modifications our ChatController looks like that.

Notice that I added and Event Handler which is thrown every time an incoming message comes via a POST event. Since the event is static, we can subscribe to it without instantiating ChatController, which is crucial for us to be able to pass the event data from the POST request to the instance ChatProxy upon every call to ChatController.

Notice we used a custom EventArgs objects. It is a regular EventArgs object, extended with a property that holds a Message object.

ChatProxy, where the heavy lifting happens

So now we, after we have covered the Message, MessageReceiver and ChatController classes, we can move on to the core part of the application, the ChatProxy class. As you recall, it is instatiated as the private property of the MainWindow class in the UI.

Let’s look at the bare-bones ChatProxy class first (without any methods).

We expose one public property called Status, which we’ll use in the GUI to check if things aren’t wrong. Other than that, we have:
– 2 delegates, which – as I already mentioned – we’ll use to call the GUI methods passed in the constructor (as seen earlier). The delegates need to be public, since they’re used in the signature of the public constructor
– an instance of HttpClient, which we’ll use for outgoing messages
– an instance of HttpSelfHostServer, which we’ll be our self-hosted ASP.NET server, and which we’ll use to accept messages incoming via POST requests (through ChatController class)

In addition to that we will expose one more public member (already mentioned earlier) – public async void SendMessage(Message m) which we’ll call from GUI to send messages.

Finally, ChatProxy also has a few private methods:

The first two will control (start/stop) the chat server. By default, we’ll start the chat server in the class’ constructor. We’ll stop upon any error. ShowMessage and ShowError are just “proxy” methods to call the delegates passed from the GUI layer to the Business layer.

Constructor in all its glory

Let’s have a look at the ChatProxy constructor:

The following happens here:
1. First of all, we’ll try to start the local self-host server. We’ll look into that code in a second, for now we’ll just say that if it happens successfully, it would update the Status property to true, otherwise it would set that to false. If the server isnt running, there is no point in continuing with anything, so we wrap the rest of the code in an “if” clause.
2. We then take the two methods passed from the GUI layer and assign to our Business layer delegates. This pattern gives us a possibility to update GUI from business layer without really knowing what the GUI methods do.
Additionally, we take the partner’s address and instantiate HttpClient using that. This is now ready and waiting to send messages. Finally, we subscribe to the static Event Handler of the ChatController, to listen to events of incoming messages. This way we are almost ready with our two way communication.

Starting the server in your application

Let’s rewind back a bit, and look into the private void StartChatServer(string myport) method.

This is probably a familiar bit of code to you, if you have read anything about the self hosting ASP.NET Web Api. Firstly we create an instance of HttpSelfHostConfiguration – through which we set our server’s url (localhost) and port, as well as define routing; for our purposes the default routing to controller’s name will suffice.

Since our controller’s name is Chatcontroller, we’ll be posting to “api/chat” whenever we send messages. Then, all that’s left is to instantiate HttpSelfHostServer and start it. We update the status property according to the exceptions (or lack thereof) and we’re ready.

For the record, let’s show here the stopChatServer() method, which is very simple

We’ll call it any time there is trouble.

Sending and receiving messages like a busy bee

Time to glue in the last piece of our puzzle – sending the message. This is surprisingly easy. Recall that by now, we have our self-hosted server up and running (so we are ready to accept incoming messages), and we have an instance of HttpClient(), pointing at our chat partner IP. So by now, all that’s left is to write a method to send the message.

Let me reiterate, why that the method is public – it’s being invoked only from the outside – in this case from the GUI layer, on the ChatProxy instance. The sending of the message is super simple, all we need to do a Post to the partner’s IP and his “api/chat” (ChatController). To do that, we call PostAsync method of the instance of HttpClient poiting to our partner, and pass the serializedMessage (form encoded). Note, that the serializedMessage property already contains properly encoded message as we populate it in the Message constructor.

If we catch some exceptions (i.e. network timeout) we close our chat server and display an error. Also, if the response code is different than 200, we’ll update the GUI with an error message.

Notice that we also call the method responsible for displaying the message in the GUI. We do that, cause in the chat area we want to see our messages as well, not just incoming ones.

Remember – we show messages and errors by invoking the delegates passed from GUI layer:

That’s practically the entire functionality in place now.

Events flow

Since our discussion so far may have not been very coherent or consistent (sorry about that folks), let’s recap what we have done. Consider the entire chain of events, from the start of the application:

1. The application opens. Self host server is not running yet.

2. User fills in username, his port and the partner’s IP and port. Clicks the “start chat” button.

3. The click instantiates ChatProxy in the MainWindow class. The methods responsible for UI updating – showing message and showing error are passed as delegates to the ChatProxy. ChatProxy will use them to update UI instead of calling methods of GUI namespace directly, without really knowing them.

4. Upon instantiating ChatProxy, the self-host server gets started. Additionally, ChatProxy instance creates a private instance of HttpClient which points at the partner’s IP, and ChatProxy instance subscribes to ChatController’s MessageArrived event which will be thrown when incoming message arrives by a POST request.
In other words the mechanisms for “send” and “receive” are on stand by.

5. Whenever user types in a message and either clicks on send or presses enter key, the appropriate event fires and calls the only public method of the ChatProxy instance – sendMessage.

6. Message gets posted via HttpClient to the partner. Message shows up in the sender’s GUI.

7. Partner’s (receiver’s) ChatController receives the POST event, and emits MessageArrived event. Partner’s ChatProxy instance is subscribed to that and calls the delegate responsible for showing the message in the GUI. By that time, message is visible in the partner’s GUI as well.

That’s about it. We have our chat! By the way, if you are interested in creating chat with WCF instead, here is an excellent tutorial.

Pre-requisites and some info

The application requires .NET 4.5 since it uses async modifier on the method used to post messages. This can be modified though and run on .NET 4.0 as well. If you try to run the chat over the network you need to allow it through your firewall. I tested with Windows Firewall switched off, and it works like a charm. Finally, chat application needs to be run with elevation (in administrator’s mode) since HttpListener, which is used by self-host server requires such.

So how does it look

1. Chatting with yourself
You configure the app to use port A and point it to localhost:A as partner’s IP. This way you’d be able to chat with yourself in one window – because you’d be posting messages to your own ChatController!

2. Chatting on same machine via different ports
You can also start two separate instances of chat application. Just make one instance use port A, and point it to partner at localhost:B, while making the other one use port B and point it to partner at localhost:A.

3. Chatting over the network
You can also chat over the network (remember – firewall!). See the screenshot below:

Summary & source files

If you read the post closely enough, you probably recall myself bragging about the potential of ditching Live and Skype. Turns out that maybe people won’t be doing that soon, but I feel that we have built something nice with ASP.NET Web Api here.

Of course, there are countless ways of taking this further and enahncing it: introducing better error handling, incorporating smoother configuration, friend lists, automatic network discovery in LAN and many many others.
Anyway, I hope you had fun with this one.

Source on github