Posts tagged ‘mochiweb’

Building an Erlang chat server with Comet – Part 3

You can see a live demo of the following tutorial here.

Overview

In the last part, we implemented the first version of our chat server. Now that the framework is down, we can start adding some more features. You can find the source for this tutorial here

In this part, we will add the following features:

  • Rate Limiting for Messages
  • Admin Login
  • Kick/Ban users
  • Limiting connections per host

Rate Limiting

This feature will *help* in stopping people from spamming the chat room. It isn’t very complex, but it will give you the idea of what rate limiting can do. The algorithm for rate limiting is very simple, if a user sends too many messages in a certain interval, stop them from sending. At the top of chat_room.erl I’ve added the following:

Continue reading ‘Building an Erlang chat server with Comet – Part 3’ »

Building an Erlang chat server with Comet – Part 2

You can see a live demo of the following tutorial at chat.tech9computers.com

Getting into the code

In the previous part, I introduced the chat system and how it should work. Now we are going to write the chat_postoffice, chat_mailbox, chat_room, and chat_web modules. Before you read download the source for this tutorial here. and dedicate about 20 minutes to read through this tutorial, because it’s kind of long (sorry).

Setting up the project

When you look at the source, take a look at the structure of the files and folders. There is a makefile to help you build and run the project. To run the server, just do the following:

$ cd erl_chat
$ make erl_chat

This will compile all the code, and start the OTP application. The web server runs on port 8000.

The Mailbox Process

Before we set up the post office, let’s define how a mailbox will work. First, it will run in its own process and keep a list of messages. Essentially it is just a loop, that accepts the following messages:

Continue reading ‘Building an Erlang chat server with Comet – Part 2’ »

Building an Erlang chat server with Comet – Part 1

Introduction

Comet is a technique to stream data, or “push” events to the web browser, instead of making the client poll the server every few seconds. This lets the client receive near real-time updates. If you want to read more about Comet, check out the article on Wikipedia, more specifically, the section about Ajax with long polling, as this is what our server will implement.

I chose web based chat because it is a great “hello world” for learning how Comet works, and I really enjoy programming chat related stuff (see my very old implementation of GameRanger)

Chat Architecture

Before we get into any code, we have to talk about how the overall chat system works. Here are a few specifications.

  • Erlang is going to handle all server-side tasks.
  • Mochiweb will be used to serve the browsers.
  • There will be only one (global) chat room.
  • When a user sends a message, all others in the room will see it.

Okay, that should do it for now. That is the very basic outline of what the chat system needs to do. Now let’s talk about the server.

Server Overview

The server will use Erlang’s OTP feature to make sure that all of our services remain running and are monitored.

We are going to have the following logical services managed by OTP:

  • chat_room
  • chat_postoffice
  • chat_web

The other modules are:

  • chat_mailbox – handles mailbox processing
  • chat_util – helper functions for the application
  • chat_sup – supervisor
  • chat_server – the chat_server application

chat_room

This module will be used to maintain the state of our chat room, and allow us to implement any “chat_room” logic, such as join/leave, sending messages, etc,.

chat_postoffice

This module is very important, it will hold all the mailboxes for each user in the chat room. Whenever we want to notify a user of something, we will send a message to the “post office”. The post office will then relay the message to an individual user’s mailbox. After the message has arrived in a user’s mailbox, the mailbox may notify our web server, and the web server can notify the client’s web browser. (I know, that’s a lot handle, but don’t worry, it will be explained).

chat_web

Finally, the chat_web module is what the web browsers will be talking to, this is the front-end of the chat system. The web module will handle the basic tasks, such as joining the chat room, sending messages, and of course, receiving messages.

Implementing chat_mailbox

In Part 2, we will implement the chat_mailbox module for our chat server. (Source code will be included).