Gopal k .Dwivedi
Gopal k .Dwivedi

Reputation: 79

How can we Find Ip Address using Javascript code?

const peerConnection = new RTCPeerConnection({ iceServers: [] });
     peerConnection.createDataChannel('');

     peerConnection.createOffer()
    .then((offer) => peerConnection.setLocalDescription(offer))
    .catch((error) => console.log(error));

    peerConnection.onicecandidate = (event) => {
    if (event.candidate) {
      console.log(`Client's IP address is ${event.candidate.address}`);
     }
   };

Upvotes: 8

Views: 39120

Answers (1)

Riyas
Riyas

Reputation: 66

Extract IP address from the candidate attribute

const ipAddress = event.candidate.candidate.split(' ')[4];

Print IP address

console.log('IP address:', ipAddress);

Upvotes: 0

Related Questions