> For the complete documentation index, see [llms.txt](https://docs.yemreak.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yemreak.com/arsiv/programming/typescript-ile-websocket-uzerinden-zamanli-bir-sekilde-mesaj-gondermek-message-queue.md).

# Typescript ile Websocket uzerinden zamanli bir sekilde mesaj gondermek, message

programming, typescript, message queue, websocket, queue

Mesaj gönderme sıklığınızı düzenlemeniz veya WebSocket bağlantınızı yönetirken bir backoff mekanizması uygulamanız önerilir. Aşağıda, mesajlar arasında gecikme ekleyerek mesaj gönderme işlemini kontrol eden basit bir TypeScript örneği sunulmaktadır. Bu örnek, mesaj gönderimini zamanlamalı bir şekilde sürdürerek buffer'ın taşmasını önlemeye yardımcı olabilir:

```typescript
import WebSocket from 'ws';

class WebSocketClient {
    private ws: WebSocket;
    private url: string;
    private messageQueue: string[] = [];
    private isSending: boolean = false;

    constructor(url: string) {
        this.url = url;
        this.connect();
    }

    private connect(): void {
        this.ws = new WebSocket(this.url);

        this.ws.on('open', () => {
            console.log('WebSocket connection established.');
            this.processQueue();
        });

        this.ws.on('message', (data) => {
            console.log('Received:', data);
        });

        this.ws.on('close', () => {
            console.log('WebSocket connection closed.');
            setTimeout(() => this.connect(), 1000); // Reconnect on close
        });

        this.ws.on('error', (err) => {
            console.error('WebSocket error:', err);
            this.ws.close();
        });
    }

    private async processQueue(): Promise<void> {
        if (this.isSending || this.messageQueue.length === 0) {
            return;
        }

        this.isSending = true;
        while (this.messageQueue.length > 0) {
            const message = this.messageQueue.shift()!;
            this.ws.send(message);
            await new Promise(resolve => setTimeout(resolve, 100)); // Delay between messages
        }
        this.isSending = false;
    }

    public send(message: string): void {
        this.messageQueue.push(message);
        this.processQueue();
    }
}

// Usage
const wsUrl = 'wss://api.someapi.com/ws/'; // Replace it with ur URL
const client = new BTSEWebSocketClient(wsUrl);
client.send(JSON.stringify({ event: 'subscribe', data: { channel: 'your_channel' } }));

```

Bu kodda, `messageQueue` adında bir kuyruk kullanılarak WebSocket üzerinden gönderilecek mesajlar sıraya alınır. `processQueue` metodu, belirli bir gecikme ile bu kuyruktaki mesajları sırayla gönderir. Böylece, sunucunun alabileceği maksimum sıklığın üzerine çıkmamış olursunuz.

Bu yaklaşım, mesaj gönderimini düzenleyerek ve buffer'ı taşırma ihtimalini azaltarak `User message buffer is full` hatasının önüne geçmeye yardımcı olur.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.yemreak.com/arsiv/programming/typescript-ile-websocket-uzerinden-zamanli-bir-sekilde-mesaj-gondermek-message-queue.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
