Author

Topic: Blockchain web sockets client working in background [need advice] (Read 615 times)

legendary
Activity: 1442
Merit: 1179
Yes, you could change it to check for inputs instead of outputs.
sr. member
Activity: 616
Merit: 263
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?)
legendary
Activity: 1442
Merit: 1179
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");
            });
      };
 };
}


sr. member
Activity: 616
Merit: 263
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
Jump to: