56199

How to use socket service in angular 6? [closed]

Question:

I am new to sockets so i am implementing socket services in angular 6 project..?

How to Use Sockets in Angular 6?

May be using some cool Socket Examples?

Libraries? Tutorials? Blog Articles?

Answer1:

<strong>How to use socket service in angular 6?</strong>

<strong>1.First you have to create component in angular like :</strong>

<strong>socketComponent.ts</strong>

import { SocketDataService } from 'socketDataService'; import { Subscription } from 'rxjs'; @Component({ selector: 'app-socket', templateUrl: './app-socket.component.html', styleUrls: ['./app-socket.component.css'], }) export class SocketComponent implements OnInit { sub: Subscription; constructor(private socketDataService: SocketDataService) {} ngOnInit() { this.getSocketData(); } getSocketData(): void { this.sub = this.socketDataService.getSocketData() .subscribe(data => { this.socketData = data }) } }

<strong>2. You have to create service like :</strong>

<strong>socketService.ts</strong>

import { Observable } from 'rxjs/Observable'; @Injectable() export class SocketDataService { private socket; constructor(private http: Http) { this.socket = io('http://localhost:3000') } observer getSocketData(): Observable<any> { this.socket.on('socket-data', (res) => { this.observer.next(res); }); return this.getSocketDataObservable(); } getSocketDataObservable(): Observable<any> { return new Observable(observer => { this.observer = observer; }); } }

Recommend

  • Error: Can't resolve all parameters for TypeDecorator: Angular 2 RC-6
  • Angular 2 router auxiliary routes not working on redirectTo
  • Pass array from parent template to child template
  • Getting different value with placeholder over CPU/GPU
  • Salesforce Different WSDL files and when to use
  • Spring: No transaction manager has been configured
  • Angular2 component view does not update on value change via method
  • AndEngine Applying Transparancy to AndEngine View
  • Specifying Castle WCF Integration Facility Endpoint Behavior per Endpoint
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • htaccess add www if not subdomain, if subdomain remove www
  • JSON response opens as a file, but I can't access it with JavaScript
  • Test if a set exists before trying to drop it
  • D3 nodes and links from JSON with nested arrays of children
  • How to create a file in java without a extension
  • Typescript - Unable to get 'import' statement to function
  • Alert pop up with LWUIT
  • NHibernate Validation Localization with S#arp Architecture
  • ilmerge with a PFX file
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Adding a button at the bottom of a table view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Read text file and split every line in MSBuild
  • req.body is undefined - nodejs
  • Counter field in MS Access, how to generate?
  • Accessing IRQ description array within a module and displaying action names
  • Is there a amazon webstore API for customers?
  • Get object from AWS S3 as a stream
  • Java applet as stand-alone Windows application?
  • Updating server-side rendering client-side
  • MySQL WHERE-condition in procedure ignored
  • Adding custom controls to a full screen movie
  • Circular dependency while pushing http interceptor
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]