# 7.7. Chat server

> Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest problems to solve?

Hints 213, 245, 271

```java
public class chatServer {
    private ArrayList<User> allUsers;

    void startChat();
    void createUser(String username){
        allUsers.push(new User(username));
    }
}

public class User {
    private int id;
    private String username;
    private ArraList<User> allContacts;
    private ArrayList<Conversation> allConversations; // should be a hashmap

    public User(String username) {
        this.id = rand();
        this.username = username;
        this.allContacts = new ArrayList();
        this.allConversations = new ArrayList();
    }
    void createConversation(String username);
    void sendMessage(String username) {
        this.allConversations[username].sendMessage();
    }
}

public class Conversation {
    private ArrayList<User> participants;
    private ArrayList<Object> sharedFiles;
}
```

## Solution

The key objects of the system are users, conversations and status messages.

* How to know if someone is online?
  * Regularly ping the client
* How do we deal with conflicting information? There's info stored in the computer's memory and some other in the database, what if they get out of sync, which one is right?
* How do we make the server scale?
  * Split the data across many servers, incresing the concern about out-of-sync data
* How do we prevent DoS attacks?

## Telegram export chat json file

Example `json` of how Telegram saves message data in a `personal chat`.

```json
{
    "name": "name",
    "type": "personal_chat",
    "id": 123456,
    "messages": [
        {   // text message
           "id": 234,
           "type": "message",
           "date": "2020-01-29T11:41:46",
           "edited": "1970-01-01T01:00:00",
           "from": "Username",
           "from_id": 123456,
           "text": "..."
        },
        {   // sticker
           "id": 432,
           "type": "message",
           "date": "2020-01-29T12:14:22",
           "edited": "1970-01-01T01:00:00",
           "from": "Username",
           "from_id": 123456,
           "file": "stickers/AnimatedSticker.tgs",
           "thumbnail": "stickers/AnimatedSticker.tgs_thumb.jpg",
           "media_type": "sticker",
           "sticker_emoji": "😄",
           "text": ""
        },
        {   // link in text message
           "id": 436093,
           "type": "message",
           "date": "2020-01-30T17:03:34",
           "edited": "1970-01-01T01:00:00",
           "from": "ane",
           "from_id": 222,
           "text": [
                {
                 "type": "link",
                 "text": "https://www.link.com"
                }
           ]
        },
        {   // photo message
            "id": 436396,
            "type": "message",
            "date": "2020-02-02T15:56:06",
            "edited": "1970-01-01T01:00:00",
            "from": "Username",
            "from_id": 123456,
            "photo": "photos/photo.jpg",
            "width": 648,
            "height": 259,
            "text": ""
        },
        {   // audio message
            "id": 36057,
            "type": "message",
            "date": "2016-04-05T13:26:02",
            "edited": "1970-01-01T01:00:00",
            "from": "Username",
            "from_id": 123456,
            "file": "voice_messages/audio_1@05-04-2016_13-26-02.ogg",
            "media_type": "voice_message",
            "mime_type": "audio/ogg",
            "duration_seconds": 36,
            "text": ""
        }
    ]
}
```

`json` data in a `group chat`.

```json
{
    "name": "group_name",
    "type": "private_group",
    "id": 111111,
    "messages": [
        {
            "id": 35966,
            "type": "service",
            "date": "2016-04-04T22:35:06",
            "edited": "1970-01-01T01:00:00",
            "actor": "ane",
            "actor_id": 222,
            "action": "create_group",
            "title": "group_name",
            "members": [
                "ane",
                "Username"
            ],
            "text": ""
        },
        {
            "id": 35968,
            "type": "message",
            "date": "2016-04-04T22:39:08",
            "edited": "1970-01-01T01:00:00",
            "from": "Username",
            "from_id": 123456,
            "text": "..."
        }
    ]
}
```
