How to add signalr to redux-observable

89 Views Asked by At

I'm creating a react app which implements SignalR. I want to use SignalR for authentication my app. But I'm stuck to add my connection to redux-observable. Here is my component.

import {switchMap, Observable, map} from 'rxjs';
import {ofType} from 'redux-observable';
import {HubConnectionBuilder, LogLevel} from "@microsoft/signalr";

const connection = new HubConnectionBuilder()
.withUrl(process.env.REACT_APP_API_HUB_URL)
.configureLogging(LogLevel.Error)
.withAutomaticReconnect()
.build();

connection.start().then((result) => {
   console.log("Connected.");
})

export const signalrEpic = action$ => action$.pipe(
   ofType('LOGIN'),
   map(async(action) => {
    console.log("Logging....");
    const {email, password, deviceID} = action.payload;
    await connection.invoke("Login", email, password, deviceID);
    return {type: 'LOGIN',};
   })
);

Should I use redux-observable or just go for redux-thunk for simple logic. Thank for any help.

0

There are 0 best solutions below