All Projects → bougarfaoui → Ng Socket Io

bougarfaoui / Ng 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 Ng Socket Io

ngx-img
No description or website provided.
Stars: ✭ 25 (-90.31%)
Mutual labels:  angular4, angular5
ionic-modal-custom-transitions
Add Custom Transitions to Ionic Modals.
Stars: ✭ 22 (-91.47%)
Mutual labels:  angular4, angular5
ngx-konami
A simple directive to add easter eggs in your Angular application 👾
Stars: ✭ 34 (-86.82%)
Mutual labels:  angular4, angular5
ngx-smart-loader
Smart loader handler to manage loaders everywhere in Angular apps.
Stars: ✭ 28 (-89.15%)
Mutual labels:  angular4, angular5
hackernews-clone
HackerNews clone built with Angular 5 using the official firebase HackerNews API
Stars: ✭ 14 (-94.57%)
Mutual labels:  angular4, angular5
ngx-print
🖨️ A plug n' play Angular (2++) library to print your stuff
Stars: ✭ 124 (-51.94%)
Mutual labels:  angular4, angular5
mean-stack
MEAN stack Mongoose, Express, Angular6, Node
Stars: ✭ 22 (-91.47%)
Mutual labels:  angular4, angular5
angularx-qrcode-sample-app
Angular5/6/7/8/9/10+ sample apps with working implementations of angularx-qrcode
Stars: ✭ 15 (-94.19%)
Mutual labels:  angular4, angular5
angular-weather-app
Angular 7 web app displays the weather report through openweathermap.org APIs
Stars: ✭ 13 (-94.96%)
Mutual labels:  angular4, angular5
ngx-image-editor
Awesome image editor for Angular 6
Stars: ✭ 74 (-71.32%)
Mutual labels:  angular4, angular5
ng-toggle
Bootstrap-styled Angular Toggle Component
Stars: ✭ 14 (-94.57%)
Mutual labels:  angular4, angular5
angular-router-demo
Angular 4|5 Router
Stars: ✭ 22 (-91.47%)
Mutual labels:  angular4, angular5
Angular4-seed
Angular 4 Seed for Angular Forms
Stars: ✭ 37 (-85.66%)
Mutual labels:  angular4, angular5
angular-progress-bar
This component allow you to easy incorporate progress-bar to angular/ionic project, providing binding and color options
Stars: ✭ 26 (-89.92%)
Mutual labels:  angular4, angular5
spring-websocket-angular6
Example for using Spring Websocket and Angular with Stomp Messaging
Stars: ✭ 18 (-93.02%)
Mutual labels:  angular4, angular5
angular-landing-page
Beautiful Angular landing page with firebase, chat, poll, dynamic features/gallary, and nice animations/scrolling.
Stars: ✭ 43 (-83.33%)
Mutual labels:  angular4, angular5
file-input-accessor
Angular directive that provides file input functionality in Angular forms.
Stars: ✭ 32 (-87.6%)
Mutual labels:  angular4, angular5
ngx-redux-core
The modern redux integration for Angular 6+
Stars: ✭ 32 (-87.6%)
Mutual labels:  angular4, angular5
angular-rollbar-source-maps
Angular 2+ implementation to upload sourcemaps to Rollbar
Stars: ✭ 17 (-93.41%)
Mutual labels:  angular4, angular5
ng2-timezone-selector
A simple Angular module to create a timezone selector using moment-timezone.
Stars: ✭ 12 (-95.35%)
Mutual labels:  angular4, angular5

ng-socket-io

Build Status npm version npm downloads

Socket.IO module for Angular 2 and 4

Install

npm install ng-socket-io

How to use

Import and configure SocketIoModule

//...
import { SocketIoModule, SocketIoConfig } from 'ng-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 'ng-socket-io';

@Injectable()
export class ChatService {

    constructor(private socket: Socket) { }

    sendMessage(msg: string){
        this.socket.emit("message", msg);
    }
    
    getMessage() {
        return this.socket
            .fromEvent("message")
            .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 'ng-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.on(eventName: string)

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, message: any, [callback: Function])

Sends a message to the server. Optionally takes a callback. 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(eventName: string): Promise

Takes an event name, and returns a Promise instead of an Observable. Works the same as once in Socket.IO.

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.

Example

You can also see this example with express.js.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

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

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

@Injectable()
class ChatService {

    constructor(private socket: Socket) { }

    sendMessage(msg: string){
        this.socket.emit("message", msg);
    }

    getMessage() {
        return this.socket
            .fromEvent<any>("message")
            .map(data => data.msg );
    }
    
    close() {
      this.socket.disconnect()
    }
}


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

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