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

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: 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://docs.yemreak.com/arsiv/programming/typescript-ile-websocket-uzerinden-zamanli-bir-sekilde-mesaj-gondermek-message-queue.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.
