OpenAI WebSocket API: रियल-टाइम streaming 75% कम लागत पर

Smart AIPI अब OpenAI के WebSocket API को रियल-टाइम, bidirectional streaming के लिए support करता है। SSE से कम latency, persistent connections, और 75% सस्ता। ऐसे connect करें।

S
Smart AIPI Team
7 मिनट पढ़ें ·
OpenAI WebSocket API: रियल-टाइम streaming 75% कम लागत पर

संक्षेप में: Smart AIPI अब OpenAI के WebSocket API को support करता है। wss://api.smartaipi.com/v1/realtime से connect करें, एक response.create event भेजें, और persistent connection पर रियल-टाइम में responses stream करें। वही models, वही protocol, 75% सस्ता।

AI models के साथ interact करने का सबसे तेज़ तरीका WebSocket streaming है। पारंपरिक HTTP requests या Server-Sent Events (SSE) के विपरीत, WebSocket आपके application और API के बीच एक persistent, bidirectional connection बनाए रखता है। हर request पर connection setup नहीं, HTTP overhead नहीं, half-duplex सीमाएँ नहीं।

Smart AIPI अब इस protocol को wss://api.smartaipi.com/v1/realtime पर support करता है — OpenAI के WebSocket API के साथ पूरी तरह compatible, और 75% कम लागत पर।

SSE के बजाय WebSocket क्यों?

AI streaming के लिए Server-Sent Events standard रहे हैं, लेकिन इनके साथ कुछ trade-offs आते हैं जिन्हें WebSocket खत्म कर देता है:

फ़ीचर SSE (HTTP) WebSocket
प्रति request connection हर बार नया connection Persistent (दोबारा उपयोग)
दिशा केवल Server → Client Bidirectional
एक connection पर कई requests नहीं हाँ
पहले token की latency ज़्यादा (नया TCP + TLS) कम (connection reuse)
किसके लिए आदर्श सरल integrations Agents, रियल-टाइम apps, high-throughput

ऐसे agent loops के लिए जो लगातार दर्जनों API calls करते हैं, persistent WebSocket connection से मिलने वाली cumulative latency बचत काफ़ी महत्वपूर्ण होती है।

यह कैसे काम करता है

WebSocket API एक event-driven protocol का पालन करता है। आप server को JSON events भेजते हैं और बदले में JSON events प्राप्त करते हैं — सब कुछ एक ही persistent connection पर।

1. Connect करें और Authenticate करें

Headers में अपनी API key के साथ WebSocket connection खोलें:

wss://api.smartaipi.com/v1/realtime
Authorization: Bearer sk-proj-your-smart-aipi-key
OpenAI-Beta: realtime=v1

2. एक Request भेजें

अपने prompt के साथ एक response.create event भेजें:

{
  "type": "response.create",
  "response": {
    "model": "gpt-5.3-codex",
    "store": false,
    "instructions": "You are a helpful assistant.",
    "input": [
      {
        "type": "message",
        "role": "user",
        "content": [
          { "type": "input_text", "text": "What is WebSocket?" }
        ]
      }
    ]
  }
}

नोट: Smart AIPI WebSocket connections के लिए store: false parameter आवश्यक है।

3. Streaming Events प्राप्त करें

जैसे-जैसे response generate होता है, server events की एक sequence वापस भेजता है:

Event विवरण
response.created Response object बनाया जा चुका है
response.output_item.added नया output item (message) शुरू हुआ
response.content_part.added किसी output item के भीतर content part शुरू हुआ
response.output_text.delta Text chunk (वास्तविक streamed content)
response.output_text.done Text output पूरा हो चुका है
response.completed पूरा response समाप्त हो गया है (terminal event)

Code Examples

Node.js

import WebSocket from "ws";

const ws = new WebSocket("wss://api.smartaipi.com/v1/realtime", {
  headers: {
    "Authorization": "Bearer sk-proj-your-key",
    "OpenAI-Beta": "realtime=v1",
  },
});

ws.on("open", () => {
  ws.send(JSON.stringify({
    type: "response.create",
    response: {
      model: "gpt-5.3-codex",
      store: false,
      input: [{
        type: "message",
        role: "user",
        content: [{ type: "input_text", text: "Hello!" }],
      }],
    },
  }));
});

ws.on("message", (data) => {
  const event = JSON.parse(data);
  if (event.type === "response.output_text.delta") {
    process.stdout.write(event.delta);
  }
  if (event.type === "response.completed") {
    console.log("\n\nDone. Usage:", event.response.usage);
    ws.close();
  }
});

Python

import asyncio
import json
import websockets

async def main():
    headers = {
        "Authorization": "Bearer sk-proj-your-key",
        "OpenAI-Beta": "realtime=v1",
    }

    async with websockets.connect(
        "wss://api.smartaipi.com/v1/realtime",
        extra_headers=headers,
    ) as ws:
        await ws.send(json.dumps({
            "type": "response.create",
            "response": {
                "model": "gpt-5.3-codex",
                "store": False,
                "input": [{
                    "type": "message",
                    "role": "user",
                    "content": [{"type": "input_text", "text": "Hello!"}],
                }],
            },
        }))

        async for message in ws:
            event = json.loads(message)
            if event["type"] == "response.output_text.delta":
                print(event["delta"], end="", flush=True)
            if event["type"] == "response.completed":
                print(f"\n\nUsage: {event['response']['usage']}")
                break

asyncio.run(main())

cURL (त्वरित टेस्ट)

एक ही command से verify करें कि WebSocket handshake सफल होता है:

curl -isN --http1.1 \
  -H "Connection: Upgrade" \
  -H "Upgrade: websocket" \
  -H "Sec-WebSocket-Version: 13" \
  -H "Sec-WebSocket-Key: dGVzdA==" \
  -H "Authorization: Bearer sk-proj-your-key" \
  -H "OpenAI-Beta: realtime=v1" \
  https://api.smartaipi.com/v1/realtime

सफल connection पर HTTP/1.1 101 Switching Protocols लौटता है।

WebSocket बनाम SSE कब उपयोग करें

दोनों protocols Smart AIPI के माध्यम से काम करते हैं। अपने use case के आधार पर चुनें:

  • SSE का उपयोग करें सरल integrations, one-off requests, और जब आप सबसे सरल implementation चाहते हों। किसी भी standard API call पर stream: true सेट करें।
  • WebSocket का उपयोग करें agent loops, interactive applications, high-frequency request patterns, और जहाँ भी consecutive calls के बीच सबसे कम संभव latency चाहिए।

Pricing

WebSocket requests का billing standard API requests की तरह ही होता है — token usage के आधार पर। 75% discount यहाँ लागू होता है:

Model OpenAI Direct Smart AIPI बचत
GPT-5.3 Codex (output) $14.00 / 1M tokens $3.50 / 1M tokens 75%
GPT-5.2 (output) $10.00 / 1M tokens $2.50 / 1M tokens 75%
Codex Mini (output) $0.60 / 1M tokens $0.15 / 1M tokens 75%

शुरू कैसे करें

  1. एक API key लेंsmartaipi.com पर sign up करें (free credits शामिल हैं, credit card की आवश्यकता नहीं)
  2. Connect करेंwss://api.smartaipi.com/v1/realtime पर WebSocket खोलें
  3. Events भेजें — अपने model और prompt के साथ response.create envelope का उपयोग करें
  4. Responses stream करें — आते ही response.output_text.delta events को process करें

यदि आप पहले से OpenAI का WebSocket API उपयोग कर रहे हैं, तो केवल URL बदलना है। बाकी सब — authentication, events, payload format — बिल्कुल समान है।

अक्सर पूछे जाने वाले सवाल

क्या Smart AIPI, OpenAI WebSocket API को support करता है?

हाँ। Authorization header में अपनी API key के साथ wss://api.smartaipi.com/v1/realtime से connect करें। यह protocol OpenAI के WebSocket Responses API के साथ पूरी तरह compatible है।

क्या WebSocket, SSE से तेज़ है?

लगातार आने वाली requests के लिए, हाँ। WebSocket एक persistent connection बनाए रखता है, जिससे TCP और TLS handshake overhead समाप्त हो जाता है जो SSE में हर नई request पर लगता है। एकल one-off requests के लिए, अंतर नगण्य है।

WebSocket पर कौन से models काम करते हैं?

Responses API के माध्यम से उपलब्ध सभी models: GPT-5.3 Codex, GPT-5.2, Codex Mini, और अन्य। response.create event में model specify करें।

क्या function calling और tool use, WebSocket पर काम करते हैं?

हाँ। पूरा Responses API feature set उपलब्ध है — function calling, tool use, structured outputs, और multi-turn conversations सब WebSocket connection पर काम करते हैं।

क्या connection time limit है?

Idle connections 15 मिनट बाद बंद कर दिए जाते हैं। आवश्यकता अनुसार periodic messages भेजें या reconnect करें। Active connections जिनमें data stream हो रहा हो, बाधित नहीं किए जाते।

क्या मैं एक connection पर कई requests भेज सकता हूँ?

हाँ। यही इसके मुख्य फ़ायदों में से एक है। एक response पूरा होने के बाद, बिना reconnect किए उसी connection पर दूसरा response.create event भेजें।

WebSocket Streaming Real-Time API
S
द्वारा लिखा गया
Smart AIPI

OpenAI-संगत API गेटवे। frontier AI models तक 75% कम लागत में पहुंचें।

मुफ़्त में शुरू करें

संदेश भेजा गया

हम 2 कार्यदिवसों के भीतर आपसे संपर्क करेंगे।

सहायता से संपर्क करें

कोई प्रश्न है या मदद चाहिए? हमें संदेश भेजें और हम 2 कार्यदिवसों के भीतर आपसे संपर्क करेंगे।