{"_id":"@bernhardb/ngx-socket-io","name":"@bernhardb/ngx-socket-io","dist-tags":{"latest":"4.8.1"},"versions":{"4.8.1":{"name":"@bernhardb/ngx-socket-io","version":"4.8.1","description":"Socket.IO module for Angular","main":"index.ts","repository":{"type":"git","url":"git+ssh://git@github.com/rodgc/ngx-socket-io.git"},"keywords":["Angular","Socket-io","TypeScript"],"author":{"name":"rodgc"},"license":"MIT","bugs":{"url":"https://github.com/rodgc/ngx-socket-io/issues"},"homepage":"https://github.com/rodgc/ngx-socket-io#readme","dependencies":{"core-js":"^3.39.0","reflect-metadata":"^0.2.2","socket.io":"^4.8.1","socket.io-client":"^4.8.1","zone.js":"~0.15.0","tslib":"^2.3.0"},"peerDependencies":{"@angular/common":"^19.0.0","@angular/core":"^19.0.0","rxjs":"^7.8.1"},"module":"fesm2022/bernhardb-ngx-socket-io.mjs","typings":"index.d.ts","exports":{"./package.json":{"default":"./package.json"},".":{"types":"./index.d.ts","default":"./fesm2022/bernhardb-ngx-socket-io.mjs"}},"sideEffects":false,"_id":"@bernhardb/ngx-socket-io@4.8.1","gitHead":"ccd86c6c97222979745112e26653931a116259e9","_nodeVersion":"20.17.0","_npmVersion":"10.8.2","dist":{"integrity":"sha512-5/P7w5LmVUa3UJh8K5/v6H/vqfGMsMimp/Q8x2R6gtYEj7ooH2OuNlL5J/hae5GEu6sNjcLd69GSWNp7BMfbsw==","shasum":"e8d9c8738dc714cbfadcf3871c44e70b3d7ba608","tarball":"https://registry.npmjs.org/@bernhardb/ngx-socket-io/-/ngx-socket-io-4.8.1.tgz","fileCount":9,"unpackedSize":30230,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDYZZ7RKK6nxIYliLZvEVLZpzwDZvdCh0P7VfEWEXwL7QIhALm+eLeJqKadxarTu5SUHrQCQXSUmoLtj3nrWKxFUWvA"}]},"_npmUser":{"name":"bernhardb","email":"bernhard.bezdek@googlemail.com"},"directories":{},"maintainers":[{"name":"bernhardb","email":"bernhard.bezdek@googlemail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ngx-socket-io_4.8.1_1732380515941_0.6891001567465256"},"_hasShrinkwrap":false}},"time":{"created":"2024-11-23T16:48:35.858Z","4.8.1":"2024-11-23T16:48:36.108Z","modified":"2024-11-23T16:48:36.359Z"},"maintainers":[{"name":"bernhardb","email":"bernhard.bezdek@googlemail.com"}],"description":"Socket.IO module for Angular","homepage":"https://github.com/rodgc/ngx-socket-io#readme","keywords":["Angular","Socket-io","TypeScript"],"repository":{"type":"git","url":"git+ssh://git@github.com/rodgc/ngx-socket-io.git"},"author":{"name":"rodgc"},"bugs":{"url":"https://github.com/rodgc/ngx-socket-io/issues"},"license":"MIT","readme":"# ngx-socket-io\n\n[![Build Status](https://travis-ci.org/rodgc/ngx-socket-io.svg?branch=master)](https://travis-ci.org/rodgc/ngx-socket-io)\n[![npm version](https://badge.fury.io/js/ngx-socket-io.svg)](https://badge.fury.io/js/ngx-socket-io)\n[![npm downloads](https://img.shields.io/badge/Downloads-132%2Fmonth-brightgreen.svg)](https://github.com/rodgc/ngx-socket-io)\n\n[Socket.IO](http://socket.io/) module for Angular\n\n## Install\n\n`npm install ngx-socket-io`\n\n**Important:**\n\nMake sure you're using the proper corresponding version of socket.io on the server.\n\n| Package Version | Socket-io Server Version | Angular version |\n|-----------------|--------------------------|-----------------|\n| v3.4.0          | v2.2.0                   |                 |\n| v4.1.0          | v4.0.0                   | 12.x            |\n| v4.2.0          | v4.0.0                   | 13.x            |\n| v4.3.0          | v4.5.1                   | 14.x            |\n| v4.4.0          | v4.5.1                   | 15.x            |\n| v4.5.0          | v4.5.1                   | 16.x            |\n| v4.6.1          | v4.7.2                   | 17.x            |\n| v4.7.0          | v4.7.2                   | 18.x            |\n| v4.8.1          | v4.8.1                   | 19.x            |\n\n## How to use\n\n### Import and configure SocketIoModule for NgModule based applications\n\n```ts\nimport { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';\n\nconst config: SocketIoConfig = { url: 'http://localhost:8988', options: {} };\n\n@NgModule({\n  declarations: [AppComponent],\n  imports: [BrowserModule, SocketIoModule.forRoot(config)],\n  providers: [],\n  bootstrap: [AppComponent],\n})\nexport class AppModule {}\n```\n\nWe need to configure `SocketIoModule` module using the object `config` of type `SocketIoConfig`, this object accepts two optional properties they are the same used here [io(url[, options])](https://socket.io/docs/v4/client-initialization/#Options).\n\nNow we pass the configuration to the static method `forRoot` of `SocketIoModule`\n\n### Import and configure SocketIoModule for standalone based applications\n\nIn app.config.ts use the following:\n\n```ts\nimport { ApplicationConfig, importProvidersFrom } from '@angular/core';\nimport { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';\n\nconst config: SocketIoConfig = { url: 'http://localhost:8988', options: {} };\n\nexport const appConfig: ApplicationConfig = {\n  providers: [\n    importProvidersFrom(SocketIoModule.forRoot(config))\n  ]\n};\n```\n\nFor standalone applications we do not have the AppModule where we can import the `SocketIoModule`. Instead we can use the `importProvidersFrom` provided by angular to provide the `SocketIoModule` to our application. The usage of the socket instance is then the same as if we used a NgModule based application.\n\n### Using your socket Instance\n\nThe `SocketIoModule` provides now a configured `Socket` service that can be injected anywhere inside the `AppModule`.\n\n```typescript\nimport { Injectable } from '@angular/core';\nimport { Socket } from 'ngx-socket-io';\nimport { map } from 'rxjs/operators';\n\n@Injectable()\nexport class ChatService {\n  constructor(private socket: Socket) {}\n\n  sendMessage(msg: string) {\n    this.socket.emit('message', msg);\n  }\n  getMessage() {\n    return this.socket.fromEvent('message').pipe(map(data => data.msg));\n  }\n}\n```\n\n### Using multiple sockets with different end points\n\nIn this case we do not configure the `SocketIoModule` directly using `forRoot`. What we have to do is: extend the `Socket` service, and call `super()` with the `SocketIoConfig` object type (passing `url` & `options` if any).\n\n```typescript\nimport { Injectable, NgModule } from '@angular/core';\nimport { Socket } from 'ngx-socket-io';\n\n@Injectable()\nexport class SocketOne extends Socket {\n  constructor() {\n    super({ url: 'http://url_one:portOne', options: {} });\n  }\n}\n\n@Injectable()\nexport class SocketTwo extends Socket {\n  constructor() {\n    super({ url: 'http://url_two:portTwo', options: {} });\n  }\n}\n\n@NgModule({\n  declarations: [\n    //components\n  ],\n  imports: [\n    SocketIoModule,\n    //...\n  ],\n  providers: [SocketOne, SocketTwo],\n  bootstrap: [\n    /** AppComponent **/\n  ],\n})\nexport class AppModule {}\n```\n\nNow you can inject `SocketOne`, `SocketTwo` in any other services and / or components.\n\n## API\n\nMost of the functionalities here you are already familiar with.\n\nThe only addition is the `fromEvent` method, which returns an `Observable` that you can subscribe to.\n\n### `socket.of(namespace: string)`\n\nTakes an namespace.\nWorks the same as in Socket.IO.\n\n### `socket.on(eventName: string, callback: Function)`\n\nTakes an event name and callback.\nWorks the same as in Socket.IO.\n\n### `socket.removeListener(eventName: string, callback?: Function)`\n\nTakes an event name and callback.\nWorks the same as in Socket.IO.\n\n### `socket.removeAllListeners(eventName?: string)`\n\nTakes an event name.\nWorks the same as in Socket.IO.\n\n### `socket.emit(eventName:string, ...args: any[])`\n\nSends a message to the server.\nWorks the same as in Socket.IO.\n\n### `socket.fromEvent<T>(eventName: string): Observable<T>`\n\nTakes an event name and returns an Observable that you can subscribe to.\n\n### `socket.fromOneTimeEvent<T>(eventName: string): Promise<T>`\n\nCreates a Promise for a one-time event.\n\nYou should keep a reference to the Observable subscription and unsubscribe when you're done with it.\nThis prevents memory leaks as the event listener attached will be removed (using `socket.removeListener`) ONLY and when/if you unsubscribe.\n\nIf you have multiple subscriptions to an Observable only the last unsubscription will remove the listener.\n\n## Know Issue\n\nFor `error TS2345` you need to add this to your `tsconfig.json`.\n\n```json\n{\n  ...\n  \"compilerOptions\": {\n    ...\n    \"paths\": {\n      \"rxjs\": [\"node_modules/rxjs\"]\n    }\n  },\n}\n```\n\n## Related projects\n\n- [bougarfaoui/ng-socket-io](https://github.com/bougarfaoui/ng-socket-io) - Socket.IO module for Angular\n\n## LICENSE\n\nMIT\n","readmeFilename":"README.md"}