Socket.io with fromEvent observable and Typescript in NodeJS

Viewed 911

I want to bind socket event with RxJs. I am fairly new in RxJs. I am using typescript with NodeJs. I tried implementing Socket.io with RxJs following some blogs but having issue with types, showing error socket.Server is not assignable to FromEventTarget<{}>.

.Rxjs 6.3.3, Node 10.14.2, Socket-io 2.2.0, Typescript 2.9.2, Express 4.16.4

 import * as socketio from "socket.io";
 import { of, fromEvent } from 'rxjs';
 import { switchMap, map, mergeMap, takeUntil, tap } from 'rxjs/operators';

  class SocketServer {
    createSocketServer(http) {
    const io$ = of(socketio(http));
            const connection$ = io$.pipe(
        switchMap((io) => {
            return fromEvent(io, 'connection').pipe( 
    // ====> this line io giving error Argument of type 'Server' is not assignable to parameter of type 'FromEventTarget<{}>'.
    // ==> Property 'off' is missing in type 'Server' but required in type 'JQueryStyleEventEmitter'.ts(2345)
                tap(res => console.log('socket conected success fully', res)),
                map(client => ({ io, client }))
            )
        })
    );
    // Stream of disconnections
    const disconnect$ = connection$.pipe(
        mergeMap(({ client }) => {
            return fromEvent(client, 'disconnect').pipe(  
    // <===== in this line showing error type client: {} not assignable to FromEventTarget<{}>
                map(() => client)
            )
        })
    );
    // On connection, listen for event
    const listen = (event) => {
        return connection$.pipe(
            mergeMap(({ io, client }) => {
                return fromEvent(client, event).pipe(
                    takeUntil(disconnect$),
                    map(data => ({ io, client, data }))
                )
            })
        )
    }
     }

 }

Looking best solution for this pattern to implement socket with RxJs with Typescript and node.

0 Answers
Related