Skip to content

Instantly share code, notes, and snippets.

@rodrigobdz
Last active February 13, 2023 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigobdz/f9969f9e49cf24ef635db4ee90bf3459 to your computer and use it in GitHub Desktop.
Save rodrigobdz/f9969f9e49cf24ef635db4ee90bf3459 to your computer and use it in GitHub Desktop.
Ping IP Address in React Native
fetch('http://localhost')
.then((response) => {
console.log('response ', response)
if (response.status === 200) {
console.log('success')
} else {
console.warn('error')
}
})
.catch((error) => {
console.error('network error: ' + error)
})
@ithieund
Copy link

This is not actually ping, this will send a http request into the server and wait for its response.
This approach will cost more resource and time than ping.

@rodrigobdz
Copy link
Author

What would you recommend instead?

@sm-hejazi
Copy link

@rodrigobdz
Copy link
Author

What about a lightweight solution? It seems oversized to build an app to ping a server.

@mrveera
Copy link

mrveera commented May 3, 2020

@rodrigobdz you are assuming web server is running in that host, it might be just ftp,dns,etc. In those cases fetch won't work

@rodrigobdz
Copy link
Author

@rodrigobdz you are assuming web server is running in that host, it might be just ftp,dns,etc. In those cases fetch won't work

@veera83372 Thanks for the heads-up

@saif71
Copy link

saif71 commented Feb 13, 2023

just a heads up. It doesn't work when you try to ping (fetch) an http URL from a https host.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment