All Projects → rodgc → Ngx Socket Io

rodgc / Ngx Socket Io

Licence: mit
Socket.IO module for Angular

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Ngx Socket Io

Angular Contacts App Example
Full Stack Angular PWA example app with NgRx & NestJS
Stars: ✭ 570 (+220.22%)
Mutual labels:  socket, socket-io, angular4
ionic-uuchat
基于ionic3,angular4的实时聊天app,兼容web端。该项目只是前端部分,所有数据需要请求后端服务器,需要配套express-uuchat-api使用。
Stars: ✭ 14 (-92.13%)
Mutual labels:  angular2, socket-io, angular4
Ionic2 Pokedex
🎮 Pokédex sample app developed with Ionic 2, Angular 2 and Apache Cordova. Using Pokéapi as source for data.
Stars: ✭ 143 (-19.66%)
Mutual labels:  angular2, angular4
Nuxt Socket Io
Nuxt Socket IO - socket.io client and server module for Nuxt
Stars: ✭ 148 (-16.85%)
Mutual labels:  socket, socket-io
Ngx Daterangepicker Material
Pure Angular 2+ date range picker with material design theme, a demo here:
Stars: ✭ 169 (-5.06%)
Mutual labels:  angular2, angular4
Angular Google Maps
Angular 2+ Google Maps Components
Stars: ✭ 1,982 (+1013.48%)
Mutual labels:  angular2, angular4
Ionic3 Components
A project full of ionic 3 components and samples - to make life easier :)
Stars: ✭ 1,689 (+848.88%)
Mutual labels:  angular2, angular4
Ionic2 Rating
⭐️ Angular star rating bar. Built for Ionic 2+.
Stars: ✭ 177 (-0.56%)
Mutual labels:  angular2, angular4
Coreui Free Bootstrap Admin Template
CoreUI is free bootstrap admin template
Stars: ✭ 11,038 (+6101.12%)
Mutual labels:  angular2, angular4
Ngx Gauge
A highly customizable Gauge component for Angular 9+ apps and dashboards
Stars: ✭ 158 (-11.24%)
Mutual labels:  angular2, angular4
Angular2 Crud Rest
Sample Angular (2.x and 4.x) app: CRUD example + routing
Stars: ✭ 152 (-14.61%)
Mutual labels:  angular2, angular4
Angular Truffle Starter Dapp
Angular CLI + Truffle Starter Dapp; write, compile & deploy smart contracts on Ethereum blockchains
Stars: ✭ 174 (-2.25%)
Mutual labels:  angular2, angular4
Ngx Inline Editor
Native UI Inline-editor Angular (4.0+) component
Stars: ✭ 172 (-3.37%)
Mutual labels:  angular2, angular4
Ng2 Search Filter
Angular 2 / Angular 4 / Angular 5 custom pipe npm module to make a search filter on any input, 🔥 100K+ downloads
Stars: ✭ 137 (-23.03%)
Mutual labels:  angular2, angular4
Amazing Time Picker
Timepicker (Clock Picker) for Angular 2, Angular 4 and Angular 5, Angular 6, Angular 7 - Compatible with Angular Material
Stars: ✭ 142 (-20.22%)
Mutual labels:  angular2, angular4
Ngx Config
Configuration utility for Angular
Stars: ✭ 135 (-24.16%)
Mutual labels:  angular2, angular4
Angular5 Iot Dashboard
Multipurpose dashboard admin for IoT softwares, remote control, user interface. Develop your client dashboards in Angular 5 with vast variety of components available.
Stars: ✭ 148 (-16.85%)
Mutual labels:  angular2, angular4
Ngx Qrcode
An Angular 9/10 Component Library for Generating QR (Quick Response) Codes
Stars: ✭ 161 (-9.55%)
Mutual labels:  angular2, angular4
Angular2 Spring
Angular 2+ and Spring Integration
Stars: ✭ 133 (-25.28%)
Mutual labels:  angular2, angular4
Webapiclientgen
Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
Stars: ✭ 134 (-24.72%)
Mutual labels:  angular2, angular4

ngx-socket-io

Build Status npm version npm downloads

Socket.IO module for Angular

Install

npm install ngx-socket-io

How to use

Import and configure SocketIoModule

import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';

const config: SocketIoConfig = { url: 'http://localhost:8988', options: {} };

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    SocketIoModule.forRoot(config)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

We 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]).

Now we pass the configuration to the static method forRoot of SocketIoModule

Using your socket Instance

The SocketIoModule provides now a configured Socket service that can be injected anywhere inside the AppModule.

import { Injectable } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { map } from 'rxjs/operators';

@Injectable()
export class ChatService {

    constructor(private socket: Socket) { }

    sendMessage(msg: string){
        this.socket.emit("message", msg);
    }
     getMessage() {
         return this.socket
             .fromEvent("message")
             .pipe(map((data) => data.msg));
    }
}

Using multiple sockets with different end points

In 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).

import { Injectable, NgModule } from '@angular/core';
import { Socket } from 'ngx-socket-io';

@Injectable()
export class SocketOne extends Socket {

    constructor() {
        super({ url: 'http://url_one:portOne', options: {} });
    }

}

@Injectable()
export class SocketTwo extends Socket {

    constructor() {
        super({ url: 'http://url_two:portTwo', options: {} });
    }

}

@NgModule({
  declarations: [
    //components
  ],
  imports: [
    SocketIoModule,
    //...
  ],
  providers: [SocketOne, SocketTwo],
  bootstrap: [/** AppComponent **/]
})
export class AppModule { }

Now you can inject SocketOne, SocketTwo in any other services and / or components.

API

Most of the functionalities here you are already familiar with.

The only addition is the fromEvent method, which returns an Observable that you can subscribe to.

socket.of(namespace: string)

Takes an namespace. Works the same as in Socket.IO.

socket.on(eventName: string, callback: Function)

Takes an event name and callback. Works the same as in Socket.IO.

socket.removeListener(eventName: string, callback?: Function)

Takes an event name and callback. Works the same as in Socket.IO.

socket.removeAllListeners(eventName?: string)

Takes an event name. Works the same as in Socket.IO.

socket.emit(eventName:string, ...args: any[])

Sends a message to the server. Works the same as in Socket.IO.

socket.fromEvent<T>(eventName: string): Observable<T>

Takes an event name and returns an Observable that you can subscribe to.

socket.fromEventOnce<T>(eventName: string): Promise<T>

Creates a Promise for a one-time event.

You should keep a reference to the Observable subscription and unsubscribe when you're done with it. This prevents memory leaks as the event listener attached will be removed (using socket.removeListener) ONLY and when/if you unsubscribe.

If you have multiple subscriptions to an Observable only the last unsubscription will remove the listener.

Know Issue

For error TS2345 you need to add this to your tsconfig.json.

{
  ...
  "compilerOptions": {
    ...
    "paths": {
      "rxjs": ["node_modules/rxjs"]
    }
  },
}

Related projects

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].