0% found this document useful (0 votes)
34 views4 pages

WebSocket Authentication and Communication Flow

This document provides a comprehensive guide to the WebSocket API for the Affiliate Service, detailing connection establishment, authentication flow, request and response types, and error handling. It includes example flows for authentication and data requests, along with best practices for using the API. The document emphasizes the importance of valid JSON payloads and managing connection states effectively.

Uploaded by

o21p1rl4e1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

WebSocket Authentication and Communication Flow

This document provides a comprehensive guide to the WebSocket API for the Affiliate Service, detailing connection establishment, authentication flow, request and response types, and error handling. It includes example flows for authentication and data requests, along with best practices for using the API. The document emphasizes the importance of valid JSON payloads and managing connection states effectively.

Uploaded by

o21p1rl4e1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WebSocket API Documentation: Affiliate Service

Table of Contents

●​ Connection Establishment
●​ Authentication Flow
●​ Request Types
●​ Response Types
●​ Error Handling
●​ Example Flows

Connection Establishment
Endpoints

Environment URL

Production [Link]

Development [Link]

Connection Initiation
const ws = new WebSocket("[Link]
Authentication Flow

Step 1: Send Authentication


[Link] = () => {​
const authMessage = {​
type: "AUTH",​
accessToken: "your_jwt_token",​
refreshToken: "your_refresh_token"​
};​
[Link]([Link](authMessage));​
};

Step 2: Handle Response

Response Type Fields Description

AUTH_SUCCESS user, accessToken, refreshToken Authentication succeeded

AUTH_FAILED reason Authentication failed (connection closes with code 4001)


[Link] = (event) => {​
const response = [Link]([Link]);​

if([Link] === "AUTH_SUCCESS") {​
// Store new tokens and user data​
[Link]('accessToken', [Link]);​
} else if([Link] === "AUTH_FAILED") {​
[Link]("Auth failed:", [Link]);​
}​
};

Request Types
After authentication, clients can send these request types:

Type Description Example Request

affiliate_activities Get dashboard activities { type: "affiliate_activities" }

affiliate_top_affiliates Get top performers { type: "affiliate_top_affiliates" }

affiliate_revenue_sha
Get revenue metrics { type: "affiliate_revenue_share" }
re

affiliate_turnover_sha
Get turnover data { type: "affiliate_turnover_share" }
re

affiliate_sub_affiliate Get sub-affiliates { type: "affiliate_sub_affiliate" }

Response Types
Server will respond with corresponding message types:

Response Type Contains Example

affiliate-revenue-share Revenue data { type: "affiliate-revenue-share", body: {...} }

affiliate-turnover-share Turnover stats { type: "affiliate-turnover-share", body: {...} }

affiliate-sub-affiliate Sub-affiliate list { type: "affiliate-sub-affiliate", body: [...] }

affiliate-top-affiliates Leaderboard { type: "affiliate-top-affiliates", body: [...] }


Error Handling
Common Errors

Code Reason Solution

4001 Unauthorized Re-authenticate with valid tokens

1002 Protocol error Verify message formatting

1008 Malformed data Ensure valid JSON payloads

Error Response Format


{​
"error": "Descriptive message",​
"code": 1002​
}

Example Flows

1. Complete Authentication Sequence


// 1. Connect​
const ws = new WebSocket("[Link]

// 2. Authenticate​
[Link] = () => {​
[Link]([Link]({​
type: "AUTH",​
accessToken: "abc123",​
refreshToken: "def456"​
}));​
};​

// 3. Handle responses​
[Link] = (e) => {​
const msg = [Link]([Link]);​

if([Link] === "AUTH_SUCCESS") {​
[Link]("Logged in as:", [Link]);​
// Request dashboard data​
[Link]([Link]({ type: "affiliate_activities" }));​
}​
};

2. Data Request Flow


function requestRevenueShare() {​
if([Link] === [Link]) {​
[Link]([Link]({​
type: "affiliate_revenue_share"​
}));​
}​
}​

// Handle response​
[Link]('message', (event) => {​
const data = [Link]([Link]);​
if([Link] === "affiliate-revenue-share") {​
renderRevenueChart([Link]);​
}​
});

Best Practices

●​ Always check readyState before sending messages


●​ Store new tokens from AUTH_SUCCESS responses
●​ Implement reconnection logic for dropped connections
●​ Close connections with [Link]() when leaving the page

Note: All message payloads must be JSON strings with a type field.

You might also like