- I cannot connect my node.js client to a test network for displaying the current blockheader on my Arduino nano. For the subscription I'm using Infura as service provider.
- Before I was using a websocket connection, but I had no idea how to use a websocket for requesting the current blockheader.
- I'd be very pleased if you could explain to me the difference between a HttpRequest subscription and a WebSocket subscription.
- At the end I would like to know what's the main task of web3 while connecting to an Ethereum node.
The current error message is as following:
Code:
var Web3 = require('web3');
var Five = require("johnny-five");
var OledJs = require('oled-js');
var Font = require('oled-font-5x7');
//connect to blockchain client
//based on infura
var web3ws = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/...mySocket'))
//load board ressources
//Initialize the board ressources
var board = new Five.Board();
var oled;
board.on("ready", function() {
//oled parameters
const opts = {width: 128, height: 64, adress: 0x3C};
//initialize display
oled = new OledJs(board, Five, opts);
oled.clearDisplay(true);
oled.update();
//wait for blockheader
listenToBlockchain();
})
//listen for blockchain events (only works on web socket connections)
function listenToBlockchain() {
//event listener
web3ws.eth.subscribe('newBlockHeaders')
.on("data", function(blockHeader){
//an dieser Stelle wird
console.log("Block: "+blockHeader.number);
output(blockHeader);
})
.on("error", function(e){
console.log("FEHLER: "+e);
})
//output function
function output(block) {
oled.setCursor(1,1);
oled.writeString(Font, 1, ''+blockHeader.number, 1, false, 2);
oled.setCursor(1,15);
oled.writeString(Font, 1, ''+blockHeader.hash,1,true,2);
}
}
Error Message:
C:\Users\andra\Desktop\ArduinoIOTblockchainsession\node_modules\web3-core-subscriptions\src\subscription.js:208
this.callback(err2, null, this);
^
TypeError: this.callback is not a function
at Subscription.subscribe (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\node_modules\web3-core-subscriptions\src\subscription.js:208:14)
at Eth.subscribe (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\node_modules\web3-core-subscriptions\src\index.js:68:39)
at listenToBlockchain (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\infura_test.js:38:13)
at Board.<anonymous> (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\infura_test.js:31:2)
at emitNone (events.js:111:20)
at Board.emit (events.js:208:7)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)