How to use eventsource - 10 common examples

To help you get started, we’ve selected a few eventsource examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github brigadecore / brigade / v2 / brigadier-polyfill / src / jobs.ts View on Github external
eventSource.addEventListener("error", (e: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any
        if (e.status) { // If the error has an HTTP status code associated with it...
          eventSource.close()
          reject(new Error(`Received ${e.status} from the API server when attempting to open job "${this.name}" log stream`))
        } else if (eventSource.readyState == EventSource.CONNECTING) {
          // We lost the connection and we're reconnecting... nbd
          this.logger.debug("Reconnecting to log stream")
        } else if (eventSource.readyState == EventSource.CLOSED) {
          // We disconnected for some unknown reason... and presumably exhausted
          // attempts to reconnect
          reject(new Error(`Encountered unknown error receiving job "${this.name}" log stream`))
        }
      })
      eventSource.addEventListener("done", () => {
github brigadecore / brigade / v2 / brigadier-polyfill / src / jobs.ts View on Github external
eventSource.addEventListener("error", (e: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any
        if (e.status) { // If the error has an HTTP status code associated with it...
          eventSource.close()
          reject(new Error(`Received ${e.status} from the API server when attempting to open job "${this.name}" status stream`))
        } else if (eventSource.readyState == EventSource.CONNECTING) {
          // We lost the connection and we're reconnecting... nbd
          this.logger.debug("Reconnecting to status stream")
        } else if (eventSource.readyState == EventSource.CLOSED) {
          // We disconnected for some unknown reason... and presumably exhausted
          // attempts to reconnect
          reject(new Error(`Error receiving job "${this.name}" status stream: ${e.message}`))
        }
      })
    })
github umaar / wiki-globe / index.js View on Github external
function onWikiData(onData) {
	console.log('Connecting to', wikimediaStreamURL);
	const es = new EventSource(wikimediaStreamURL);
	es.addEventListener('message', onMessage(onData));
}
github CodeCommission / subscriptions-transport-sse / src / client.js View on Github external
.then(data => {
        const subId = data.subId;

        const evtSource = new EventSource(`${this.url}/${subId}`, {
          headers
        });
        this.subscriptions[subId] = {options, handler, evtSource};

        evtSource.onmessage = e => {
          const message = JSON.parse(e.data);
          switch (message.type) {
            case 'SUBSCRIPTION_DATA':
              this.subscriptions[subId].handler(message.data);
              break;
            case 'KEEPALIVE':
              break;
          }

          evtSource.onerror = e => {
            console.error(
github ElementsProject / lightning-charge-client-js / src / client.js View on Github external
stream() {
    const es = new EventSource(this.url + '/payment-stream')
    es.on('message', msg => es.emit('payment', JSON.parse(msg.data)))
    return es
  }
github automategreen / home-controller / lib / Insteon / index.js View on Github external
setTimeout(function () {
      if (es.readyState === EventSource.CLOSED) {
        that.emit('error', err);
        that.close(true);
      }
    }, 100);
  };
github olegakbarov / facebook-messenger-devkit / index.js View on Github external
source.addEventListener('error', e => {
  if (e.readyState === EventSource.CLOSED) {
    console.log('Connection was closed! ', e);
  } else {
    console.log('An unknown error occurred: ', e);
  }
}, false);
github sanity-io / sanity / packages / @sanity / cli / src / actions / login / login.js View on Github external
function getAuthChannel(baseUrl, provider, iv) {
  const uuid = crypto.randomBytes(16).toString('hex')
  const listenUrl = `${baseUrl}/v1/auth/listen/${provider.name}/${uuid}?iv=${iv}`
  return new EventSource(listenUrl)
}
github vtex / toolbelt / src / courier.js View on Github external
export const listen = (account, workspace, level, id) => {
  const es = new EventSource(`${colossusHost}/${account}/${workspace}/logs?level=${level}`)
  es.onopen = () => {
    log.debug(`Connected to logs with level ${level}`)
  }

  es.addEventListener('message', (msg) => {
    const {body: {message, code}, level, subject, sender} = JSON.parse(msg.data)
    if (subject.startsWith(`${manifest.vendor}.${manifest.name}`) || subject.startsWith('-')) {
      const suffix = id === sender ? '' : ' ' + chalk.gray(sender)
      log.log(levelAdapter[level] || level, `${(message || code || '').replace(/\n\s*$/, '')}${suffix}`)
    }
  })

  es.onerror = (err) => {
    log.error(`Connection to log server has failed with status ${err.status}`)
  }
}
github mongodb / stitch-js-sdk / packages / server / core / src / core / internal / net / NodeFetchStreamTransport.ts View on Github external
return new Promise((resolve, reject) => 
        new EventSourceEventStream(
          new EventSource(request.url),
          stream => resolve(stream),
          error => reject(error),
          retryRequest ? 
            () => retryRequest().then(es => es as EventSourceEventStream)
            : undefined
          )
      );

eventsource

W3C compliant EventSource client for Node.js and browser (polyfill)

MIT
Latest version published 2 years ago

Package Health Score

75 / 100
Full package analysis