Skip to content

Gateway

nin0dev edited this page Oct 22, 2024 · 3 revisions

The Gateway is at nin0chat's core, so you should know everything about it.

Connecting

The URL is wss://chatws.nin0.dev. (WebSocket)

Structure & opcodes

Anything that the Gateway will send you will be in the following format:

{
    "op": 0,
    "d": {
        "relevant": ["data", "here"]
    }
}

Which means the opcode will determine how to process data, the data being in "d". You are expected to send data in the same format. Here's how you can do so, in simple steps:

Heartbeating

The most important part. When you receive an opcode 2 message, immediately respond with an opcode 2 message and no data. This is the heartbeat designed to clean up offline clients. You have until the next heartbeat to send one back.

TLDR: when you receive { "op": 2, d: {} }, immediately send back { "op": 2, d: {} }.

Logging in

While you will receive incoming messages by just being connected to the Gateway, you won't be able to send any without logging in. nin0chat supports guest login, meaning an auth token isn't necessarily required.

The opcode for this operation is 1.

As a guest

Send opcode 1 with as data, an object with the anon property set to true, the username property set to whichever username you'd like and device to web, mobile or bot.

Warning

Do not connect your bot as a guest. It will be IP-banned.

As a registered user/bot

Send opcode 1 with as data, an object with the anon property set to false, the token property set to a user or bot token and device to web, mobile or bot.

Tip

Tokens can be obtained through the REST API.

Once either is done

You will get an op 1 response containing the user info in this format:

id: string;
username: string;
roles: string;

Tip

You can see more about roles on the Roles page.

Receiving and sending messages

Messages are sent from the Gateway to clients with the opcode 0, and are received with this data:

userInfo: {
    username: string;
    roles: number;
    id: string;
},
timestamp: bigint;
content: string;
id: string;
device: "web" | "mobile" | "bot";

Sending messages is even more easy. Just send op 0 with in the data object the content property, with, you guessed it, message content!

Warning

Unregistered users are capped to 300 characters, registered/bots at 1000.

Opcode 3 (message history)

You will get this opcode when connecting. It is simply the latest 100 messages for you to display. The data object contains a history array. You can treat its elements as incoming messages, they're the same.

Member list (opcode 4)

If you receive opcode 4, its data will be structured as:

{
    users: [
               { id: string; username: string; }
    ]
}

Use this to update the member list. As of now, clicking on a name should copy its user ID.

Errors

These are handled with opcode -1 and message passed in the data object.

Clone this wiki locally