Tutorial: Creating a Chat Server Using Mailboxes
Applies to: Functional Programming
Published: January 2010
Summary: This tutorial demonstrates how to develop a web-based chat application that uses F# agents to store the state of the chat room.
This article is associated with Real World Functional Programming: With Examples in F# and C# by Tomas Petricek with Jon Skeet from Manning Publications (ISBN 9781933988924, copyright Manning Publications 2009, all rights reserved). No part of these chapters may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, electrostatic, mechanical, photocopying, recording, or otherwise—without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
This tutorial presents a larger example that uses an agent-based architecture to create a web chat application. Using agents for developing servers has two main advantages. First, agents offer an easy and safe way to create concurrent applications. A real-world online chat may have hundreds of chat rooms. To enable concurrent processing of requests that involve individual rooms, the server could represent each room as a separate agent. Second, agents very efficiently use threads because they are asynchronous.
The following links provide more information about asynchronous workflows and about server-side development using F# in general:
This step creates a thread-safe agent that stores messages in a chat room. The agent can add new messages and return the contents of a chat room.
This step takes an existing agent and encapsulates it into a .NET object. The object exposes members for synchronous and asynchronous access and a member that makes the agent usable from C#.
This step shows how to use the HttpListener type to create a type that can be used for developing an agent-based HTTP web server.
This step combines the components from the previous steps to builds a simple but fully functional agent-based online chat.
This tutorial demonstrates how to use agents to develop one particular server-side application. More information about server-side programming with agents in general and for various design patterns that involve agents can be found in the following articles:
This article is based on Real World Functional Programming: With Examples in F# and C#. Book chapters related to the content of this article are:
Book Chapter 13: “Asynchronous and data-driven programming” explains how asynchronous workflows work and uses them to write an interactive script that downloads a large dataset from the Internet.
Book Chapter 14: “Writing parallel functional programs” explains how to use the Task Parallel Library to write data-parallel and task-based parallel programs. This approach complements agent-based parallelism in F#.
Book Chapter 16: “Developing reactive functional programs” discusses how to write reactive user interfaces using asynchronous workflows and events. This approach is related to agents but more suitable for creating user interfaces.
To download the code snippets shown in this article, go to http://code.msdn.microsoft.com/Chapter-2-Concurrent-645370c3