network-programminglocalhosthostingself-hosting

Accessing localhost API endpoint from different machine


I have a pressure sensor plugged into my computer, and the only way to collect the data is through a localhost API endpoint, meaning right now only that machine can collect data. Is there any way to receive data from the localhost API on a different machine? I also need to ping the API 20-40 times a second if that matters.


Solution

  • There are couple of ways I can think of, I am assuming both the machines are on same network

    1. Use localhost API to collect the data in database and create a GET endpoint inside same application for fetching the data according your parameters. You can access GET endpoint from different machine by hitting network ip address of your local machine. Which you can check using ifconfig command in your terminal, check en0 type where you will find something like 192.168.X.X. From other machine you can hit http://192.168.X.X:<port>/getData, where <port> is the localhost port.

    2. If you don't want to use database, then you can use publish subscribe mechanism which is real time. see Autobahn Python

    How publish subscribe works ?

    You will have to make your localhost machine a publisher (server) which will publish events or sensor data in your case (real time). The other machine will be subscriber (client ) which will listen to the events from your server and do necessary processing.
    

    Its uses WAMP (Web application messaging protocol) for communication. The sample code for basic publisher and subsriber can be found here.