यह पृष्ठ मशीन द्वारा अनुवादित है। अंग्रेज़ी संस्करण मूल स्रोत है और अधिक सटीक या अधिक अद्यतन हो सकता है। अंग्रेज़ी में देखें

ब्राउज़र क्लाइंट

आप नेटिव WebSocket API से ब्राउज़र से कनेक्ट कर सकते हैं। किसी लाइब्रेरी की ज़रूरत नहीं है। यह पेज एक पूरा क्लाइंट दिखाता है जो सब्सक्राइब करता है, कनेक्शन को चालू रखता है और दोबारा कनेक्ट करता है।


यह सर्वर क्लाइंट से कैसे अलग है

  • कुंजी URL में जाती है। ब्राउज़र WebSocket पर हेडर सेट नहीं कर सकते, इसलिए x-api-key हेडर की जगह ?apiKey= इस्तेमाल करें।
  • आप नहीं देख सकते कि कनेक्शन क्यों अस्वीकार हुआ। ब्राउज़र अस्वीकार हुए कनेक्शन का HTTP स्टेटस नहीं दिखाता। सॉकेट खुलने से पहले ही कोड 1006 के साथ बंद हो जाता है। गलत कुंजी, बिना WebSocket वाला प्लान और भरी हुई कनेक्शन सीमा, सब एक जैसे दिखते हैं। लगातार कुछ विफल प्रयासों के बाद रुकें और डैशबोर्ड में कुंजी और प्लान जाँचें।
  • बाकी सब एक जैसा है। subscribe संदेश भेजें, हर 25 सेकंड में ping करें, कंट्रोल फ़्रेम संभालें और ज़रूरत पड़ने पर दोबारा कनेक्ट करें। लाइफ़साइकल और सीमाएँ देखें।

कनेक्शन URL

स्ट्रीमURL
एनरिच्डwss://wss.finlight.me?apiKey=YOUR_API_KEY
रॉwss://wss.finlight.me/raw?apiKey=YOUR_API_KEY

वैकल्पिक क्वेरी पैरामीटर: takeover=true सीमा पर होने पर आपके सबसे पुराने निष्क्रिय कनेक्शन को बदल देता है, और clientVersion=... आपके क्लाइंट को लेबल देता है। मानों को URL-एन्कोड करें। कनेक्ट करें देखें।


WEBSOCKETब्राउज़र

पूरा उदाहरण

यह क्लाइंट:

  • URL में कुंजी के साथ कनेक्ट करता है और subscribe संदेश भेजता है,
  • हर 25 सेकंड में ping करता है और 60 सेकंड तक pong न आने पर दोबारा कनेक्ट करता है,
  • 1 से 30 सेकंड तक बढ़ते इंतज़ार के साथ दोबारा कनेक्ट करता है, और pong या लेख के बाद इंतज़ार रीसेट करता है,
  • लगातार 5 अस्वीकार हुए कनेक्शनों के बाद, VALIDATION_FAILED के बाद और connection_limit preemption के बाद रुक जाता है,
  • quota_exceeded preemption के बाद एक घंटा इंतज़ार करता है,
  • डुप्लिकेट फ़्रेम हटाता है, रीकनेक्ट के बाद का शुरुआती पुश भी,
  • 2 घंटे की सीमा से पहले, 115 मिनट बाद दोबारा कनेक्ट करता है। इससे एक छोटा अंतराल बनता है। अगर आपको हर लेख चाहिए, तो इसे REST से भरें; लाइफ़साइकल और सीमाएँ देखें।

यह v3 फ़्रेम और वे v2 कंट्रोल फ़्रेम दोनों पढ़ता है जो सर्वर पहले subscribe से पहले भेजता है।

ब्राउज़र क्लाइंट

WEBSOCKET
wss://wss.finlight.me
const API_KEY = 'YOUR_API_KEY' // tests only, see the note above
const BASE_URL = 'wss://wss.finlight.me' // raw stream: 'wss://wss.finlight.me/raw'

const SUBSCRIBE = {
  payloadVersion: 3,
  query: 'ticker:AMD',
  language: 'en',
  include: ['companies', 'events'], // leave out on the raw stream
  includeUpdates: true,
  clientNonce: crypto.randomUUID(),
}

function startStream({ onArticle, onStatus = console.log }) {
  let ws
  let pingTimer
  let watchdog
  let rotateTimer
  let delay = 1000
  let refusedInARow = 0
  let stopped = false
  const seen = new Set()

  const resetWatchdog = () => {
    clearTimeout(watchdog)
    watchdog = setTimeout(() => ws.close(4000, 'no pong'), 60_000)
  }

  const stop = (why) => {
    stopped = true
    onStatus(`stopped: ${why}`)
    ws?.close(1000)
  }

  const scheduleReconnect = (waitMs) => {
    onStatus(`reconnecting in ${Math.round(waitMs / 1000)} s`)
    setTimeout(connect, waitMs)
  }

  function connect() {
    if (stopped) return
    let opened = false
    let nextWait = null // set by frames that need a special wait
    ws = new WebSocket(`${BASE_URL}?apiKey=${encodeURIComponent(API_KEY)}`)

    ws.onopen = () => {
      opened = true
      refusedInARow = 0
      ws.send(JSON.stringify(SUBSCRIBE))
      pingTimer = setInterval(() => ws.send(JSON.stringify({ action: 'ping', t: Date.now() })), 25_000)
      rotateTimer = setTimeout(() => {
        nextWait = 0
        ws.close(1000, 'rotate')
      }, 115 * 60_000)
      resetWatchdog()
    }

    ws.onmessage = (message) => {
      const frame = JSON.parse(message.data)
      const name = frame.event ?? frame.action // v2 control frames use "action"
      const data = frame.data ?? frame

      switch (name) {
        case 'article.created':
        case 'article.updated': {
          delay = 1000
          resetWatchdog()
          const key = `${name}|${data.id}|${JSON.stringify(data)}`
          if (seen.has(key)) return
          seen.add(key)
          if (seen.size > 5000) seen.delete(seen.values().next().value)
          onArticle(name, data)
          break
        }
        case 'connection.pong':
        case 'pong':
          delay = 1000
          resetWatchdog()
          break
        case 'connection.admitted':
        case 'admit':
          onStatus(`admitted, lease ${data.leaseId}`)
          break
        case 'connection.preempted':
        case 'preempted':
          if (data.reason === 'connection_limit') stop('another connection took over')
          else nextWait = 60 * 60_000 // quota_exceeded: try again in one hour
          break
        case 'connection.error':
          if (data.code === 'VALIDATION_FAILED') stop(`subscribe rejected: ${JSON.stringify(data.details)}`)
          else onStatus(`server error ${data.code}: ${data.message}`)
          break
        default:
          // "error" in v2 form, or an event this client does not know yet
          onStatus(`ignored frame ${name}`)
      }
    }

    ws.onclose = (event) => {
      clearInterval(pingTimer)
      clearTimeout(watchdog)
      clearTimeout(rotateTimer)
      if (stopped) return
      if (!opened && ++refusedInARow >= 5) {
        stop('connection refused 5 times: check the API key and your plan')
        return
      }
      if (event.code === 1008) {
        stop('closed by the server with code 1008')
        return
      }
      const wait = nextWait ?? delay
      delay = Math.min(delay * 2, 30_000)
      scheduleReconnect(wait)
    }
  }

  connect()
  return { close: () => stop('closed by you') }
}

// Usage
const stream = startStream({
  onArticle: (event, article) => console.log(event, article.id, article.title),
})
// later: stream.close()

आपको दिखने वाले संदेश

फ़्रेममतलब
connection.admittedआपका पहला subscribe पहुँच गया। यह सर्वर के subscribe जाँचने से पहले भेजा जाता है।
article.createdएक नया मेल खाने वाला लेख। subscribe के ठीक बाद का पहला लेख सबसे नया मौजूदा मेल है।
article.updatedकिसी मेल खाने वाले लेख में बदलाव। केवल includeUpdates: true के साथ।
connection.pongआपके ping का उत्तर।
connection.preemptedसर्वर यह कनेक्शन बंद करता है। reason देखें।
connection.errorआपके संदेश में समस्या। code देखें।
{"action": ...}v2 रूप में एक कंट्रोल फ़्रेम, आपके पहले subscribe से पहले भेजा गया।

पूरी सूची और त्रुटि कोड के लिए फ़्रेम देखें।


आगे क्या