title: How to get the socket-id in Angular using Socket-io.client
body: <p>In my angular app I am using <code>socket.io-client</code> npm package to make a socket-io communication to another node-server. </p>
  
  <p>I have the following code forthe same. </p>
  
  <pre><code>import { Injectable } from '@angular/core';
  
  import { Observable } from 'rxjs/Observable';
  import * as io from 'socket.io-client';
  
  @Injectable({
    providedIn: 'root'
  })
  export class DataService {
  
   constructor() { }
   private url = 'http://localhost:3000';
   private socket;
  
  
   getLiveData1() {
     let observable = new Observable(observer =&gt; {
      this.socket = io(this.url);
      console.log("THIS SOCKET IS:getLiveData1");
      this.socket.on('connect', function() {
       console.log("on connect:THIS SOCKET IS id is");
  
       console.log(this.socket.id);
  
       console.log(this.socket.socket.id);
      });
      this.socket.on('message', (data) =&gt; {
      observer.next(data);
    });
    return () =&gt; {
      this.socket.disconnect();
    }
  })
  return observable;
  }
  </code></pre>
  
  <p>I am trying to access the client id only on the <code>connect</code> event. 
   this.socket.on('connect', function() {
       console.log("on connect:THIS SOCKET IS id is");</p>
  
  <pre><code> console.log(this.socket.id);
  
   console.log(this.socket.socket.id);
  });
  </code></pre>
  
  <p>however both the log-statements where i am trying to log the socket-id using : <code>this.socket.id or this.socket.socket.id</code> errors out saying that <code>this.socket</code> is <code>undefined</code>
  <strong>How can i get the client-side socket-id in this case?</strong> </p>