Flex, C# and Asynchronous Sockets Chat

18.02.2008

I’ve previously written about the wonders of Rich Internet Applications (RIAs), and I’ve have been looking at both Silverlight and Adobe Flex. For now, at least, I go with Flex, because it’s the mature and complete of the two.

After being sidetracked for a while with my love for casinos and games, I thought it would be appropriate to return to something more business-friendly. So, for my next project, I have chosen to implement a chat client/server with Flex on the frontend and C# on the backend. It could be used for customer service or be a community feature on a website.

I was thinking about doing the whole thing with Web Services, but that has a couple of shortcomings.

First of all, Web Services are stateless; Consuming a Web Service means sending one request from a client to a server and receiving one response. Each request/response pair is separate from all others - stateless. A chat application needs to be more “connected” the entire time, so that it’s possible to push things from either end to the other without the client having to continuously poll the server.

Secondly, Web Services include a bit of overhead in terms of SOAP envelopes, and requires code-generation (in Flex), etc., and I wanted something clean and fast. Ok, sure, my chat application probably won’t be used by thousands of simultaneous users, but why go for the Kia when you can have the Ferrari at more or less the same implementation cost?

The fastest ride in town is “asynchronous sockets”, and the easiest and cleanest implementation of that is using TCP. And luckily, it’s completely supported by both Flex and C#, so that’s what I went with.

Chat Client

http://www.topholt.com/ChatClient/bin-debug/ChatClient.html

A Few Implementation Details 

The chat client above is setup to use port 8000 for communications, so your firewall should allow you to make TCP connections to that port if you want to try it out. It shouldn’t be a big deal. If you download the code, you can modify it to use whatever port you like.

All communications are currently unsecured, which means no encryption, no authentication, no authorization and no data checks of any kind. The downside is that anyone can mess with the chat, but the upside is that you, dear readers, can grab the code and easily modify it for your own needs.

The application’s communications architecture is simple: Each time someone says something, a 256 byte UTF-8 encoded datagram is sent to the server and then immediately distributed to all connected clients, including the sender. That way, everyone sees what everyone is writing.

Edit: View Source on the Flash/Flex file to get the client code or download the zip. And get the C# server source code here. It’s built with VS 2008 and compiled for .NET 3.5, but should work just as fine with 2.0.

Edit 2: I’m currently having server hosting problems, so I am getting ready to move my stuff to a new provider. Therefore, the chat server may be offline at times. The code is still available for download, though.