### File

`src/lib/socket-io.service.ts`

### Index

###### Properties

- emptyConfig
- eventObservables$
- ioSocket
- subscribersCounter

###### Methods

- connect
- disconnect
- emit
- fromEvent
- fromOneTimeEvent
- of
- on
- once
- removeAllListeners
- removeListener

### Constructor

`constructor(config: SocketIoConfig)`
Defined in src/lib/socket-io.service.ts:15

Parameters :

Name | Type | Optional
config | `SocketIoConfig` | No

### Properties

emptyConfig
Type : `SocketIoConfig`
Default value : `{ url: '', options: {}, }`
Defined in src/lib/socket-io.service.ts:12

eventObservables$
Type : `Record<string | Observable<any>>`
Default value : `{}`
Defined in src/lib/socket-io.service.ts:10

ioSocket
Type : `any`
Defined in src/lib/socket-io.service.ts:11

subscribersCounter
Type : `Record<string | number>`
Default value : `{}`
Defined in src/lib/socket-io.service.ts:9

### Methods

connect
`connect()`
Defined in src/lib/socket-io.service.ts:39
Returns : `any`

disconnect
`disconnect(_close?: any)`
Defined in src/lib/socket-io.service.ts:43

Parameters :

Name | Type | Optional
_close | `any` | Yes

Returns : `any`

emit
`emit(_eventName: string, ..._args: any[])`
Defined in src/lib/socket-io.service.ts:47

Parameters :

Name | Type | Optional
_eventName | `string` | No
_args | `any[]` | No

Returns : `any`

fromEvent
`fromEvent(eventName: string)`
Defined in src/lib/socket-io.service.ts:59

Type parameters :

- T

Parameters :

Name | Type | Optional
eventName | `string` | No

Returns : `Observable<T>`

fromOneTimeEvent
`fromOneTimeEvent(eventName: string)`
Defined in src/lib/socket-io.service.ts:83

Type parameters :

- T

Parameters :

Name | Type | Optional
eventName | `string` | No

Returns : `Promise<T>`

of
`of(namespace: string)`
Defined in src/lib/socket-io.service.ts:27

Parameters :

Name | Type | Optional
namespace | `string` | No

Returns : `void`

on
`on(eventName: string, callback: (data: T) => void)`
Defined in src/lib/socket-io.service.ts:31

Type parameters :

- T

Parameters :

Name | Type | Optional
eventName | `string` | No
callback | `function` | No

Returns : `void`

once
`once(eventName: string, callback: (data: T) => void)`
Defined in src/lib/socket-io.service.ts:35

Type parameters :

- T

Parameters :

Name | Type | Optional
eventName | `string` | No
callback | `function` | No

Returns : `void`

removeAllListeners
`removeAllListeners(_eventName?: string)`
Defined in src/lib/socket-io.service.ts:55

Parameters :

Name | Type | Optional
_eventName | `string` | Yes

Returns : `any`

removeListener
`removeListener(_eventName: string, _callback?: () => void)`
Defined in src/lib/socket-io.service.ts:51

Parameters :

Name | Type | Optional
_eventName | `string` | No
_callback | `function` | Yes

Returns : `any`

    import { Observable } from 'rxjs';
    import { share } from 'rxjs/operators';

    import * as io from 'socket.io-client';

    import { SocketIoConfig } from './socket-io.config';

    export class WrappedSocket {
      subscribersCounter: Record<string, number> = {};
      eventObservables$: Record<string, Observable<any>> = {};
      ioSocket: any;
      emptyConfig: SocketIoConfig = {
        url: '',
        options: {},
      };

      constructor(private config: SocketIoConfig) {
        if (config === undefined) {
          config = this.emptyConfig;
        }
        const url: string = config.url;
        const options: any = config.options;
        const ioFunc = (io as any).default ? (io as any).default : io;
        this.ioSocket = ioFunc(url, options);
      }

      of(namespace: string) {
        this.ioSocket.of(namespace);
      }

      on<T>(eventName: string, callback: (data: T) => void) {
        this.ioSocket.on(eventName, callback);
      }

      once<T>(eventName: string, callback: (data: T) => void) {
        this.ioSocket.once(eventName, callback);
      }

      connect() {
        return this.ioSocket.connect();
      }

      disconnect(_close?: any) {
        return this.ioSocket.disconnect.apply(this.ioSocket, [ _close ]);
      }

      emit(_eventName: string, ..._args: any[]) {
        return this.ioSocket.emit.apply(this.ioSocket, [ _eventName, ..._args ]);
      }

      removeListener(_eventName: string, _callback?: () => void) {
        return this.ioSocket.removeListener.apply(this.ioSocket, [ _eventName, _callback ]);
      }

      removeAllListeners(_eventName?: string) {
        return this.ioSocket.removeAllListeners.apply(this.ioSocket, [ _eventName ]);
      }

      fromEvent<T>(eventName: string): Observable<T> {
        if (!this.subscribersCounter[eventName]) {
          this.subscribersCounter[eventName] = 0;
        }
        this.subscribersCounter[eventName]++;

        if (!this.eventObservables$[eventName]) {
          this.eventObservables$[eventName] = new Observable((observer: any) => {
            const listener = (data: T) => {
              observer.next(data);
            };
            this.ioSocket.on(eventName, listener);
            return () => {
              this.subscribersCounter[eventName]--;
              if (this.subscribersCounter[eventName] === 0) {
                this.ioSocket.removeListener(eventName, listener);
                delete this.eventObservables$[eventName];
              }
            };
          }).pipe(share());
        }
        return this.eventObservables$[eventName];
      }

      fromOneTimeEvent<T>(eventName: string): Promise<T> {
        return new Promise<T>(resolve => this.once(eventName, resolve));
      }
    }

# results matching ""

# No results matching ""