Bitcoin Forum
May 07, 2024, 01:20:15 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Blockchain web sockets client working in background [need advice]  (Read 614 times)
boy130 (OP)
Sr. Member
****
Offline Offline

Activity: 616
Merit: 263



View Profile
January 14, 2017, 03:40:06 PM
 #1

Hi,

I need to relate my web app with that api: https://blockchain.info/api/api_websocket .

I need that my web app can get realtime notifications about new transaction to my wallet and then send 'GET' request for one php script.

So I need to have websockerts client working in background (i think it will node.js script) and which will get all notifications and then send special 'get' request to my php script (php will update mysql database).

How it can be done? I found socket.io but not sure if their client can work in background (on server side).

Does anybody know node.js apps or php which can be usable in that situation?

Thanks.  Smiley
If you want to be a moderator, report many posts with accuracy. You will be noticed.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715044815
Hero Member
*
Offline Offline

Posts: 1715044815

View Profile Personal Message (Offline)

Ignore
1715044815
Reply with quote  #2

1715044815
Report to moderator
1715044815
Hero Member
*
Offline Offline

Posts: 1715044815

View Profile Personal Message (Offline)

Ignore
1715044815
Reply with quote  #2

1715044815
Report to moderator
1715044815
Hero Member
*
Offline Offline

Posts: 1715044815

View Profile Personal Message (Offline)

Ignore
1715044815
Reply with quote  #2

1715044815
Report to moderator
coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1179



View Profile WWW
January 17, 2017, 02:57:17 AM
 #2

Yes.
Create a nodejs file that subscribes to the websocket feed.
Then check each new entry to see if it matches what you're looking for. If it does then initiate the request module to hit your PHP page.

npm install both the websocket and request modules.

Code:
npm install ws --save
npm install request --save

Code:
var WebSocket = require("ws");
var request = require("request");
var btcs = new WebSocket('wss://ws.blockchain.info/inv');

btcs.onopen = function()
{
btcs.send( JSON.stringify( {"op":"unconfirmed_sub"} ) );
};

btcs.onmessage = function(onmsg)
{
var response = JSON.parse(onmsg.data);
        var outAddr = response.x.out[0].addr;
        if(outAddr == "1BitcoinEaterAddressDontSendf59kuE")
        {
            request({
                url: "https://somesite.com/page.php",
                json: true
            }, function(err, res, body){
            if(err){
                console.log(err);
                }
            console.log(res);
            console.log("PHP page ran");
            });
        }
}

The above way is checks ALL transactions, so it's kinda wasteful.
It would be better to subscribe to an address instead of all transactions.

Code:
var WebSocket = require("ws");
var request = require("request");
var btcs = new WebSocket('wss://ws.blockchain.info/inv');

var address = "1BitcoinEaterAddressDontSendf59kuE";
var btcs = new WebSocket("wss://ws.blockchain.info/inv");
btcs.onopen = function(){
   btcs.send(JSON.stringify({“op”:”addr_sub”, “addr”:address}));
};
btcs.onmessage = function(onmsg)
{
 var response = JSON.parse(onmsg.data);
 var getOuts = response.x.out;
 var countOuts = getOuts.length;
 for(i = 0; i < countOuts; i++)
 {
   //check every output to see if it matches specified address
   var outAdd = response.x.out[i].addr;
   var specAdd = address;
      if (outAdd == specAdd)
      {
      var amount = response.x.out[i].value;
            //transaction received
            //include a parameter with the amount in satoshis if you want
            request({
                url: "https://somesite.com/page.php?value="+amount,
                json: true
            }, function(err, res, body){
            if(err){
                console.log(err);
                }
            console.log(res);
            console.log("PHP page ran");
            });
      };
 };
}



boy130 (OP)
Sr. Member
****
Offline Offline

Activity: 616
Merit: 263



View Profile
January 19, 2017, 01:03:13 AM
 #3

Yes.
Create a nodejs file that subscribes to the websocket feed.
Then check each new entry to see if it matches what you're looking for. If it does then initiate the request module to hit your PHP page.

npm install both the websocket and request modules.

Code:
npm install ws --save
npm install request --save

Code:
var WebSocket = require("ws");
var request = require("request");
var btcs = new WebSocket('wss://ws.blockchain.info/inv');

btcs.onopen = function()
{
btcs.send( JSON.stringify( {"op":"unconfirmed_sub"} ) );
};

btcs.onmessage = function(onmsg)
{
var response = JSON.parse(onmsg.data);
        var outAddr = response.x.out[0].addr;
        if(outAddr == "1BitcoinEaterAddressDontSendf59kuE")
        {
            request({
                url: "https://somesite.com/page.php",
                json: true
            }, function(err, res, body){
            if(err){
                console.log(err);
                }
            console.log(res);
            console.log("PHP page ran");
            });
        }
}

The above way is checks ALL transactions, so it's kinda wasteful.
It would be better to subscribe to an address instead of all transactions.

Code:
var WebSocket = require("ws");
var request = require("request");
var btcs = new WebSocket('wss://ws.blockchain.info/inv');

var address = "1BitcoinEaterAddressDontSendf59kuE";
var btcs = new WebSocket("wss://ws.blockchain.info/inv");
btcs.onopen = function(){
   btcs.send(JSON.stringify({“op”:”addr_sub”, “addr”:address}));
};
btcs.onmessage = function(onmsg)
{
 var response = JSON.parse(onmsg.data);
 var getOuts = response.x.out;
 var countOuts = getOuts.length;
 for(i = 0; i < countOuts; i++)
 {
   //check every output to see if it matches specified address
   var outAdd = response.x.out[i].addr;
   var specAdd = address;
      if (outAdd == specAdd)
      {
      var amount = response.x.out[i].value;
            //transaction received
            //include a parameter with the amount in satoshis if you want
            request({
                url: "https://somesite.com/page.php?value="+amount,
                json: true
            }, function(err, res, body){
            if(err){
                console.log(err);
                }
            console.log(res);
            console.log("PHP page ran");
            });
      };
 };
}




Hi,

Thank you for your reply.

Could you tell me please will it works also with outgoing payments? (e.g. transactions from subscribed wallet?)
coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1179



View Profile WWW
January 19, 2017, 01:45:18 AM
 #4

Yes, you could change it to check for inputs instead of outputs.

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!