Real-time chat rooms
Rooms are views of messages together with a form that allows users to add a new message. When a new message is added, it will be sent to all other views connected to the same view in real time.
Real-time chat rooms can be used for two different scenarios:
- Chat rooms tied to a particular topic, possibly just one, that can be joined by multiple logged in users
- Direct messages between pairs of users.
Other use cases for chat rooms are not possible right now but will be developed later.
Creating a room requires three tables:
- The room table. This table does not require any particular fields, but is a representation of a particular room. That is, each row in this table is a distinct room.
- The messages table, which contains the individual messages. This should have a field that is a foreign key to the room table, and also a key to user table to denote the sender. Any other fields should represent the contents of the message
- The participants table. This table is a join table between room and users, representing who is allowed to participate in a room. It should have a foreign key to the room table, and a foreign key to the users table
These tables do not have to be created exclusively for the purposes stated above, for instance if you are creating a book discussion group then the table for books could serve as the room table.
You also need to create two views of the messages table
- The messages show view, which shows an existing message
- The messages form view, which is a form (Edit viewtemplate) to create new messages
Once these tables and views are in place, you can create a view of the Room view template. You have to specify the relation to the participant and message tables, and of views for showing messages and the form for creating a new message.
Once you have created the room view, you need to link to it in the context of a specific row in the room table. Linking merely to the room view will not work as you need to view a particular room, not all rooms in general. In this sense the room views works like the show views. For instance, you could create a list of the rooms using the List view template, and add a column which is a link to another view, choosing the room view. Alternatively, if you only have one room and you know the ID for the corresponding row in the room table, you can create a link such as /view/MyRoom?id=1. Here, 1 is the row id in the room table.