Realtime updates

All updates that are going through socket connection use SocketMessage entity.

public struct SocketMessage {
    
    public let account: AccountType
    public let type: Type
    public let payload: Payload
}
1
2
3
4
5
6

Each SocketMessage belongs to separate account type (personal / business) and have one of 3 types.

public extension SocketMessage {

    enum `Type` {
    
        case command
        case reply
        case update
    }
}

public extension SocketMessage {

    enum Payload {
        
        case command(Command)
        case reply(Reply)
        case update(Update)
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

command socket messages are for sending event to server. reply socket messages are for marking socket update as processed on server. update socket messages are those which inform application of realtime updates happened (e. g. new message received, new member joined the group, other user profile updated, etc).

Handling updates

Each update received and processed by the app should be marked as processed to make server know that this update shouldn't be sent to client again. So basic handling order is: app receives SocketMessage of update, app sends SocketMessage with reply time back.

SocketMessages with update type can be received in two ways:

  • Socket connection is established and update happens. In this case update message is sent to the client right away at the moment of creation.
  • Socket connection isn't established. In this case server collects updates on its side and sends them all to the client ones socket connection is established. That's why we need to send messages with reply type back to the server - to make it not send these already processed by clients events on next socket connection.

App establishes socket connection on app start and on app entering foreground. App closes socket connection on app entering background.

Update types

Each socket message of update type consists of ref (the one that we're sending back to server in a reply message), silent flag (indicator of do we need to show in-app notification about this update), action (enum with new, update, delete cases indicating action type) and payload (enum that contains updated entity type and entity(es)).

public extension SocketMessage {
    
    struct Update {
        
        public let ref: String
        public let silent: Bool
        public let action: Action
        public let payload: Payload
    }
}
1
2
3
4
5
6
7
8
9
10

Updates are handled within UpdateActions.swift file (ManageUpdate action). There are other specific actions called based on update's action and payload (e. g. we have some other users profiles deleted - client receives update socket message with delete action and payload containing array of profiles that was deleted).