Git Product home page Git Product logo

Comments (4)

Macil avatar Macil commented on August 24, 2024 3

Kefir observables generally shouldn't be thought of as objects supporting both read and write operations. Think of the Kefir observable as just representing the read side.

I frequently do things like this:

class WebSocketConnection {
  constructor(socket) {
    this._socket = socket;
    this.stream = Kefir.stream(emitter => {
      socket.on('data', e => emitter.value(e));
      socket.on('end', () => emitter.end());
    });
  }

  write(value) {
    this._socket.write(value);
  }

  close() {
    this._socket.close();
  }
}

const wsc = new WebSocketConnection(...);
wsc.stream
  .filter(value => value.type === 'bar')
  .onValue(value => {
    console.log('got bar event', value);
    console.log('responding');
    wsc.write({type: foo, data: value.data + 1});
  });

Note a small difference between @mAAdhaTTah's code and mine: his closes the socket when no one is listening. Depending on your use-case, that may be what you want. But if you don't always have listeners (generally in a very idiomatic reactive codebase you set up a bunch of listeners at the start that stick around, but if you're just using Kefir here and there in a more classic object-oriented codebase that may not be the case) and still want to write to the connection at any time, then you may not want to automatically close it in that case and instead want an explicit close method like here.

from kefir.

mAAdhaTTah avatar mAAdhaTTah commented on August 24, 2024

Loosely speaking:

const socket$ = Kefir.stream(emitter => {
  const socket = createSocket();
  socket.on('event', e => emitter.value(e));
  return () => socket.close();
});

from kefir.

mAAdhaTTah avatar mAAdhaTTah commented on August 24, 2024

I'm going to close this issue, since it's not a bug, but feel free to ask follow-up questions!

from kefir.

amireldor avatar amireldor commented on August 24, 2024

I know this is long closed, but I found that the following code also works and I felt it's too cool not to share.

// I imported fromEvents directly from 'kefir'
let myWsStream = fromEvents(websocket, 'message')

from kefir.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.