Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server-sent events client implementation #372

Open
MykhailoMatiiasevychKlarna opened this issue May 29, 2023 · 1 comment
Open

Server-sent events client implementation #372

MykhailoMatiiasevychKlarna opened this issue May 29, 2023 · 1 comment

Comments

@MykhailoMatiiasevychKlarna

Server-sent events client implementation

Purpose

I want to bump with the community and understand if this feature would be interesting and useful in scope of mappersmith.

Docs

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events

Use case

Even though the technology is well known and supported by browsers it seems to be rarely used in backend-2-backend communication.
With the boom of chatGPT this become a case as it is supporting streaming using Server-sent events: https://platform.openai.com/docs/api-reference/chat/create

Suggested implementation

Currently payload is available as a text when the request is completed.
My implementation suggestion would be to pass eventEmitter as a parameter to resource method and emit events from inside mappersmith on every new chunk of data.

import { EventEmitter } from 'node:events';
const events = new EventEmitter();

events.on('data', (data) => {
  console.log(data);
})

client.Resource.method({ eventEmitter: events })
@klippx
Copy link
Collaborator

klippx commented Jun 12, 2023

I think it looks very interesting and sounds like a fun project 👍
Since gateways are configurable I guess ideally it should be implemented in both HTTP and Fetch gateways, even though HTTP is the default it is not uncommon to override it to Fetch for node:

import forge, { configs } from 'mappersmith';
import fetchGateway from 'mappersmith/gateway/fetch';
import fetch from 'node-fetch';

configs.gateway = fetchGateway;
configs.fetch = fetch;
const client = forge({ ... })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants