Broccoli Products home | contact us | about us
(by Lou Burne, last updated 10-APRIL-2012)
Tinychat
Project Summary
Tinychat is a popular online chatroom service with a friendly browser frontend, underneath which text messages are streamed using RTMP.

Tinychat happens to be very efficient.  Even though they have millions of users, message posting is practically instantaneous.

Tinychat's website can be found at www.tinychat.com.

If we could connect to Tinychat's RTMP server, we can use their service to add chatroom functionality to our own application.
Technical Hurdles
RTMP Connection Parameters Tinychat has many servers for hosting all of the chatrooms, so it is not possible to hardcode the RTMP parameters into our application.  We have to discover these RTMP parameters for ourselves.  Fortunately there is a lookup page on the Tinychat website that returns an Xml document which contains a TcUrl for each chatroom.  We can use this to  convert a chatroom name into a set of real RTMP parameter.

Transport on Stream Id 0 The data that flows from the RTMP server includes the chatroom text messages, and details of who has entered or left the chatroom.  This is all done on a stream with an id of zero, "stream0", which is automatically opened by the server when a connection is made.  We will have to use an automatic stream mounting callback to jump on this stream when it is opened so we can access it as if we had created it ourselves.
The Code
The Tinychat.zip contains three C# files:

Tinychat.cs The application functions.  The main application function is described below.
TinychatHelper.cs A concrete class that will convert a chatroom name into a set of RTMP parameters. 

Tinychat has a rather odd way of encoding strings, so TinychatHelper also has two functions for converting a string to and from the Tinychat string format.
WebsiteRequestHelper.cs A static class for loading a webpage as a string or an XML document.

To build these files, use the most recent build of the RtmpClient, which can be found in the file archive.

Before running the application, change the ChatroomName variable to the name of a Tinychat chatroom you have created.

Main Function (Main)
In the code you will see sections have been marked A, B, C etc...

A Construct a TinychatHelper object.  The TinychatHelper constructor takes a chatroom name and populated the object with a set of RTMP parameters.
B Create a new RtmpClient object. The RtmpClient constructor takes one parameter, a reference to the main application window. Since our application runs in a console, which is windowless, this parameter can be null.

EventsEnabled - Set EventsEnabled to false so the RtmpClient doesn't waste time composing events when no one is listening.
C Create a new Rtmp connection. The RtmpConnection object returned encapsulates a connection to the remote RTMP server.

The behavior flags instruct the RtmpClient library to automatically respond to any request for pings or data acknowledgements that come from the server.
D Register for automatic mounting of new streams.

When the connection to Tinychat is opened  the stream with id of zero, "stream0", is automatically opened. 

We need to do things with this stream, but we can't open it again to get a reference to it.  Instead, we instruct the RtmpClient that any new streams opened by the server should be wrapped as a RtmpStream object as if we had opened the stream ourselves.

The function RegisterAutomaticStreamMountingCallback is where we set up the callback function for automatic mounting.  Instead of defining a separate callback function we are using an anonymous function (=>).  If you are not familiar with anonymous functions, this will look a bit peculiar, but they are worth getting used to as they allow for more compact code, and negate having to worry about getting object references into a static callback function.
E Register for pre-processing of all RTMP messages arriving on stream0.

These RTMP messages will include chatroom text messages and details of who is entering or leaving the chatroom.  We use this callback to pass these messages to our function _fnStream0Callback.

More details about this function below.
F Connect to the RTMP application.

Tinychat uses a few more parameters than the standard RTMP connection, so we have created a standard connection message, and then customized it by adding extra parameters to it.
G All that remains is to take commands through the console from the user.

In the loop, the user can enter "exit" to close the application, otherwise whatever they enter is transmitted to the chatroom.

Callback Function (_fnStream0Callback)

A The chatroom messages we are interested in are of type CommandAmf0.
B Chatroom messages start with a verb, for example, "joins" for details of who is in the chatroom, and "privmsg" for a text message.
C Verb "privmsg" indicates a text message, either sent by another guest in the chatroom, or sent by us to the chatroom.

The textual component of this message is encoded into comma delimited ASCII.  There are two functions in the TinychatHelper.cs file to encode and encode these strings.
D Verb "notice" indicates a message contained a textual message that can be printed straight to the console.
Contact form 
Use the contact form to send comments and requests for information to Broccoli Products.
Topic:
Message:
Email:
Broccoli Products Ltd © 1998-2012 Broccoli Products Ltd
Reg Number: 2895355
Reg Office: 27 Old Gloucester Street, London. WC1N 3AX
Privacy Policy
Copyright Notice
Liability Disclaimer
Contact Us