How to create a web chat with APE, Rails and MySQL

With the comet server APE it’s easy to create a web chat for your rails app. In the following howto you’ll find out how to use the user data of your rails app with restful_authentication also for your new chat. The javascript chat client will look like this demo.

Dependencies: installed APE-Server, ready rails app with restful_authentication using mysql

Scenario: You want to create a simple web chat for your rails app. Only logged in users should be able to use the chat using their name found in the mysql database.

Step 1 – Creating the APE server module

Because we’re using restful_authentication as authentication method, our server module has to have the ability to encrypt the password on its own. With this encrypt function, the module is able to validate the user and password.

First of all we need the individual encryption data from your rails app. So check /railsapp/config/initializers/site_keys.rb for the REST_AUTH_SITE_KEY and REST_AUTH_DIGEST_STRETCHES, e.g.:

REST_AUTH_SITE_KEY         = 'fecaf1261215db131593926217ddcb80191fd7e2'
REST_AUTH_DIGEST_STRETCHES = 10

Now get the server module example made by me and replace the rest_auth-variables with your individual site key and digest stretches. Also edit the mysql user, password and database of your rails environment. Put the easychat.js module in your /var/ape/examples directory and replace nickname.js with easychat.js in your /var/ape/main.ape.js file. Restart aped.

Note: In the authentication process we don’t give the raw password to APE for more security. We encrypt the already encrypted password with the restful_auth-mechanism. Our module is able to decrypt this double encrypted password and compare the encrypted passwords given by the client with the encrypted password in the user database.

Step 2 – Creating a rails controller and a view for the chat

Let’s generate a rails controller and a view for the chat:

./script/generate controller chat index

For the view we need the ape-jsf and javascript chat client. Put the ape-jsf-folder in /railsapp/public, this will make the framework reachable via http://yoursite.com/ape-jsf. We use the demo chat client found in /public/ape-jsf/Demos/Chat with a slight modification for our authentication process. Make a copy of this folder in /public/ape-jsf/Demos/easyChat and overwrite the demo.js with this modified version. Don’t forget to edit your /public/ape-jsf/Demos/config.js to make it fit your domain name.

Now we’re almost done. What we need is a controller and a view, where the user data is given to the ape and the chat client is loaded.
Replace your chat_controller.rb with this modified one. And the index.html.erb view with this modified version.

Step 3 – Chat!

Restart your APE server and rails environment. Now logged in users are able to chat via http://site.com/chat
This is a very basic setup with some problems (e.g. when connecting with a bad session id, the chat client can’t handle it, etc.). But I hope it’s a good starting point for your chat application.

Have fun chatting!

One Response to “How to create a web chat with APE, Rails and MySQL”

  1. just what i need! awesome, thanks!

Leave a Reply


linkboost