# Websockets

## Kargate Socket.IO API Documentation

### Overview

The Kargate Socket.IO API provides real-time updates for notifications and new feeds. The base URL for the API is:

```
https://devio.kargate.site
```

All requests require an **Authorization** header with a **Bearer Token**, formatted as:

```
Authorization: Bearer YOUR_TOKEN_HERE
```

***

### Endpoints

#### 1. `/notification`

**Event: `notification_history`**

Upon the first connection, the server emits a `notification_history` event, returning the user's past notifications as:

```json
[{
    "date": "2025-03-04T12:00:00Z",
    "noti_title": "New Post Available",
    "noti_type": "PostUploaded",
    "noti_message": "A new car post has been uploaded.",
    "noti_for_user": "user_123",
    "actions": [
        {
            "action_label": "View Post",
            "url": "https://kargate.site/post/456"
        }
    ]
}]
```

**Event: `notification`**

When a new notification arrives for the connected user, the server emits a `notification` event containing a `UserNotification` object:

```json
{
    "date": "2025-03-04T12:30:00Z",
    "noti_title": "Your ID verification was approved!",
    "noti_type": "IdVerifyFormApproved",
    "noti_message": "You are now a verified user.",
    "noti_for_user": "user_123",
    "actions": null
}
```

#### 2. `/newfeeds`

**Event: `newfeeds`**

Returns an array of posts (`Vec<Post>`) which can be either `CarPost` or `PackagePost`. The client should check the `post_type` field to determine the type.

Example response:

```json
[{
    "date": "2025-03-04T13:00:00Z",
    "title": "Available Car for Transport",
    "description": "A truck available from Yangon to Mandalay.",
    "author": "John Doe",
    "author_id": "user_789",
    "post_type": "Car",
    "owner_id": "owner_456",
    "driver_id": "driver_321",
    "car_id": "car_999",
    "start_location_detail": "Yangon, Myanmar",
    "start_location": [16.8409, 96.1735],
    "end_location_detail": "Mandalay, Myanmar",
    "end_location": [21.9162, 95.9560],
    "expected_date_to_start": "2025-03-06T08:00:00Z",
    "post_photos": ["https://kargate.site/img1.jpg"],
    "main_package_types": ["Electronics", "Furniture"],
    "cost_per_type": {
        "Electronics": 5000,
        "Furniture": 7000
    }
}]
```

***

### **Client-Side Implementation Example**

#### **JavaScript Implementation**

**Connecting to the API**

```javascript
const io = require("socket.io-client");

const socket = io("https://devio.kargate.site", {
    extraHeaders: {
        Authorization: "Bearer YOUR_BEARER_TOKEN"
    }
});
```

**Listening for Notifications**

```javascript
socket.on("notification_history", (notifications) => {
    console.log("Past Notifications:", notifications);
});

socket.on("notification", (newNotification) => {
    console.log("New Notification Received:", newNotification);
});
```

**Listening for New Feeds**

```javascript
socket.on("newfeeds", (posts) => {
    posts.forEach(post => {
        if (post.post_type === "Car") {
            console.log("Car Post:", post);
        } else if (post.post_type === "Package") {
            console.log("Package Post:", post);
        }
    });
});
```

#### **Flutter/Dart Implementation**

**Connecting to the API**

```dart
import 'package:socket_io_client/socket_io_client.dart' as io;

void main() {
  final socket = io.io('https://devio.kargate.site', <String, dynamic>{
    'extraHeaders': {'Authorization': 'Bearer YOUR_BEARER_TOKEN'},
    'transports': ['websocket'],
  });

  socket.connect();
}
```

**Listening for Notifications**

```dart
socket.on('notification_history', (data) {
  print('Past Notifications: $data');
});

socket.on('notification', (data) {
  print('New Notification Received: $data');
});
```

**Listening for New Feeds**

```dart
socket.on('newfeeds', (data) {
  for (var post in data) {
    if (post['post_type'] == 'Car') {
      print('Car Post: $post');
    } else if (post['post_type'] == 'Package') {
      print('Package Post: $post');
    }
  }
});
```

***

### **Conclusion**

The Kargate Socket.IO API provides a seamless way to receive real-time notifications and updates on new transportation-related posts. By utilizing event listeners, clients can stay up-to-date with the latest changes efficiently.

For more details, contact [**@linuswalker**](https://t.me/linuswalker)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apidoc.kargate.site/websockets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
