syntax = "proto3";

package protocol;

message Error {
    uint32 code = 1;
    string message = 2;
}

enum MethodType {
    CONNECT = 0;
    SUBSCRIBE = 1;
    UNSUBSCRIBE = 2;
    PUBLISH = 3;
    PRESENCE = 4;
    PRESENCE_STATS = 5;
    HISTORY = 6;
    PING = 7;
    SEND = 8;
    RPC = 9;
    REFRESH = 10;
    SUB_REFRESH = 11;
}

message Command {
    uint32 id = 1;
    MethodType method = 2;
    bytes params = 3;
}

message Reply {
    uint32 id = 1;
    Error error = 2;
    bytes result = 3;
}

enum PushType {
    PUBLICATION = 0;
    JOIN = 1;
    LEAVE = 2;
    UNSUB = 3;
    MESSAGE = 4;
    SUB = 5;
}

message Push {
    PushType type = 1;
    string channel = 2;
    bytes data = 3;
}

message ClientInfo {
    string user = 1;
    string client = 2;
    bytes conn_info = 3;
    bytes chan_info = 4;
}

message Publication {
    uint32 seq = 1;
    uint32 gen = 2;
    string uid = 3;
    bytes data = 4;
    ClientInfo info = 5;
    uint64 offset = 6;
}

message Join {
    ClientInfo info = 1;
}

message Leave {
    ClientInfo info = 1;
}

message Unsub {
    bool resubscribe =1;
}

message Sub {
    bool recoverable = 1;
    uint32 seq = 2;
    uint32 gen = 3;
    string epoch = 4;
    uint64 offset = 5;
    bool positioned = 6;
}

message Message {
    bytes data = 1;
}

message ConnectRequest {
    string token = 1;
    bytes data = 2;
    map<string, SubscribeRequest> subs = 3;
    string name = 4;
    string version = 5;
}

message ConnectResult {
    string client = 1;
    string version = 2;
    bool expires = 3;
    uint32 ttl = 4;
    bytes data = 5;
    map<string, SubscribeResult> subs = 6;
}

message RefreshRequest {
    string token = 1;
}

message RefreshResult {
    string client = 1;
    string version = 2;
    bool expires = 3;
    uint32 ttl = 4;
}

message SubscribeRequest {
    string channel = 1;
    string token = 2;
    bool recover = 3;
    uint32 seq = 4;
    uint32 gen = 5;
    string epoch = 6;
    uint64 offset = 7;
}

message SubscribeResult {
    bool expires = 1;
    uint32 ttl = 2;
    bool recoverable = 3;
    uint32 seq = 4;
    uint32 gen = 5;
    string epoch = 6;   
    repeated Publication publications = 7;
    bool recovered = 8;
    uint64 offset = 9;
    bool positioned = 10;
}

message SubRefreshRequest {
    string channel = 1;
    string token = 2;
}

message SubRefreshResult {
    bool expires = 1;
    uint32 ttl = 2;
}

message UnsubscribeRequest {
    string channel = 1;
}

message UnsubscribeResult {}

message PublishRequest {
    string channel = 1;
    bytes data = 2;
}

message PublishResult {}

message PresenceRequest {
    string channel = 1;
}

message PresenceResult {
    map<string, ClientInfo> presence = 1;
}

message PresenceStatsRequest {
    string channel = 1;
}

message PresenceStatsResult {
    uint32 num_clients = 1;
    uint32 num_users = 2;
}

message HistoryRequest {
    string channel = 1;
    bool use_since = 2;
    uint64 offset = 3;
    string epoch = 4;
    bool use_limit = 5;
    int32 limit = 6;
}

message HistoryResult {
    repeated Publication publications = 1;
    string epoch = 2;
    uint64 offset = 3;
}

message PingRequest {
}

message PingResult {
}

message RPCRequest{
    bytes data = 1;
    string method = 2;
}

message RPCResult {
    bytes data = 1 ;
}

message SendRequest{
    bytes data = 1;
}
