Code Monkey home page Code Monkey logo

Comments (11)

jeffbski avatar jeffbski commented on July 28, 2024 6

Here is an example using redux-logic with websockets https://jsfiddle.net/jeffbski/baqz3rkj/

It basically sends whatever msg you type to the websocket service at echo.websocket.org and then it adds all the responses (which are the same since it is an echo service) to a list and displays them.

from redux-logic.

jeffbski avatar jeffbski commented on July 28, 2024 1

Great idea! I will add one.

from redux-logic.

jeffbski avatar jeffbski commented on July 28, 2024 1

FYI. I have a more recent example using redux-logic@2 and rxjs@6

https://codesandbox.io/s/m38l7n745y

from redux-logic.

gihrig avatar gihrig commented on July 28, 2024

I would be interested in such an example as well.

I have a project that will require this feature, but I won't be working on that for a month or more, so an example would be good place to start.

from redux-logic.

therealgilles avatar therealgilles commented on July 28, 2024

Hi, I am trying to use/modify this code. I want the client to send back a message when it receives a specific message from the server. I am dispatching the response message sending with store.dispatch in msgRec before returning { type: MSG_REC }. But this seems to lead to msgRec being called again with the same message.

Note that I have also merged SEND_MSG_CHANGED and MSG_SEND together, as my message is not send through a form field.

I am thinking that sending the message triggers MSG_LISTEN and somehow calls msgRec again with the same message. Any advice would be appreciated. Thank you.

from redux-logic.

therealgilles avatar therealgilles commented on July 28, 2024

Nevermind, I had missed that getState().wsConnected would not work in my case, as I am using a selector. I replaced it with selector.wsConnected(getState()) and things started working properly.

from redux-logic.

jeffbski avatar jeffbski commented on July 28, 2024

Excellent! Thanks for updating us @therealgilles

from redux-logic.

therealgilles avatar therealgilles commented on July 28, 2024

@jeffbski: The example is missing error handling through failType. How should I add it back in? Same question about the websocket connection status.

from redux-logic.

jeffbski avatar jeffbski commented on July 28, 2024

You could add error handling into the example which would be useful if they have wrong url or to see why the connection is hanging up. We are tracking and displaying the connection status.

from redux-logic.

therealgilles avatar therealgilles commented on July 28, 2024

From what I can see, the example is not tracking the connection status, it's tracking the state of the connect/disconnect button, which is not the same thing.

I am not sure how to implement error handling. If you could point me to an example or documentation, it would be great.

The older version of the example also had a retry mechanism in case of disconnection, which is missing here. In my app, it means I have to manually hit refresh to reconnect the socket, whereas it was automatic before.

I am trying to see if I can adapt the older version to rxjs@6 but I am not yet well versed on it. Any help/advice will be appreciated.

from redux-logic.

therealgilles avatar therealgilles commented on July 28, 2024

Here is what I came up with. Feel free to suggest improvements.

const wsListenLogic = createLogic({
  type: actions.connect,
  cancelType: actions.disconnect,
  latest: true, // take latest only
  warnTimeout: 0, // long running logic

  processOptions: {
    failType: actions.wsError,
  },

  process({ action$, cancelled$ }) {
    const wsSubject$ = webSocket({
      url: WS_SERVER,
      openObserver: {
        next: () => store.dispatch({ type: actions.connect }),
      },
      closeObserver: {
        next: () => store.dispatch({ type: actions.disconnect }),
      },
    })

    // send message on WS_MSG_SEND action
    action$
      .pipe(
        filter(action => action.type === actions.sendMsg.toString()),
        tap(action => wsSubject$.next(getState().messageToSend)),
        takeUntil(cancelled$)
      )
      .subscribe()

    // dispatch msgReceived with payload from server
    // on any incoming messages
    // returning obs subscribes to it
    return wsSubject$.pipe(
      map(msg => actions.msgReceived(msg)),
      retryWhen(errors => errors.pipe(
        tap(err => console.error(err)),
        switchMap(err => timer(1000)),
      )),
    )
  }
})

from redux-logic.

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.