|
| 1 | +.. _eth-subscribe: |
| 2 | + |
| 3 | +======== |
| 4 | +web3.eth.subscribe |
| 5 | +======== |
| 6 | + |
| 7 | +.. code-block:: javascript |
| 8 | +
|
| 9 | + web3.eth.subscribe(type [, callback]); |
| 10 | +
|
| 11 | +The ``web3.eth.subscribe`` function lets you subscribe to specifc events in the blockchain. |
| 12 | + |
| 13 | +---------- |
| 14 | +Parameters |
| 15 | +---------- |
| 16 | + |
| 17 | +1. ``String`` - The subscription, you want to subscribe to. |
| 18 | +2. ``Mixed`` - (optional) Optional additional parameters, depending on the subscription type. |
| 19 | +3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. Will be called for each incoming subscription. |
| 20 | + |
| 21 | +.. _eth-subscription-return: |
| 22 | + |
| 23 | +------- |
| 24 | +Returns |
| 25 | +------- |
| 26 | + |
| 27 | +``EventEmitter`` - A Subscription instance |
| 28 | + |
| 29 | + - ``subscription.id``: The subscription id, used to identify and unsubscribing the subscription. |
| 30 | + - ``subscription.unsubscribe([callback])``: Unsubscribes the subscription and returns `TRUE` in the callback if successfull. |
| 31 | + - ``on("data")`` returns ``Object``: Fires on each incoming log with the log object as argument. |
| 32 | + - ``on("changed")`` returns ``Object``: Fires on each log which was removed from the blockchain. The log will have the additional property ``"removed: true"``. |
| 33 | + - ``on("error")`` returns ``Object``: Fires when an error in the subscription occours. |
| 34 | + |
| 35 | +---------------- |
| 36 | +Callback returns |
| 37 | +---------------- |
| 38 | + |
| 39 | +- ``Mixed`` - depends on the subscription, see the different subscriptions for more. |
| 40 | + |
| 41 | +------- |
| 42 | +Example |
| 43 | +------- |
| 44 | + |
| 45 | +.. code-block:: javascript |
| 46 | +
|
| 47 | + var subscription = web3.eth.subscribe('logs', { |
| 48 | + address: '0x123456..', |
| 49 | + topics: ['0x12345...'] |
| 50 | + }, function(error, result){ |
| 51 | + if (!error) |
| 52 | + console.log(log); |
| 53 | + }); |
| 54 | +
|
| 55 | + // unsubscribes the subscription |
| 56 | + subscription.unsubscribe(function(error, success){ |
| 57 | + if(success) |
| 58 | + console.log('Successfully unsubscribed!'); |
| 59 | + }); |
| 60 | +
|
| 61 | +
|
| 62 | +------------------------------------------------------------------------------ |
| 63 | + |
| 64 | +web3.eth.subscribe('pendingTransactions') |
| 65 | +===================== |
| 66 | + |
| 67 | +.. code-block:: javascript |
| 68 | +
|
| 69 | + web3.eth.subscribe('pendingTransactions' [, callback]); |
| 70 | +
|
| 71 | +Subscribes to incoming pending transactions. |
| 72 | + |
| 73 | +---------- |
| 74 | +Parameters |
| 75 | +---------- |
| 76 | + |
| 77 | +1. ``String`` - ``"pendingTransactions"``, the type of the subscription. |
| 78 | +2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. Will be called for each incoming subscription. |
| 79 | + |
| 80 | +------- |
| 81 | +Returns |
| 82 | +------- |
| 83 | + |
| 84 | +``EventEmitter``: An :ref:`subscription instance <eth-subscription-return>` as an event emitter with the following events: |
| 85 | + |
| 86 | +- ``"data"`` returns ``Object``: Fires on each incoming pending transaction. |
| 87 | +- ``"error"`` returns ``Object``: Fires when an error in the subscription occours. |
| 88 | + |
| 89 | +For the structure of the returned object see :ref:`web3.eth.getTransaction() return values <eth-gettransaction-return>`. |
| 90 | + |
| 91 | +---------------- |
| 92 | +Callback returns |
| 93 | +---------------- |
| 94 | + |
| 95 | +1. ``Object|Null`` - First parameter is an error object if the subscription failed. |
| 96 | +2. ``Object`` - The block header object like above. |
| 97 | + |
| 98 | +------- |
| 99 | +Example |
| 100 | +------- |
| 101 | + |
| 102 | + |
| 103 | +.. code-block:: javascript |
| 104 | +
|
| 105 | + var subscription = web3.eth.subscribe('pendingTransactions', function(error, result){ |
| 106 | + if (!error) |
| 107 | + console.log(transaction); |
| 108 | + }) |
| 109 | + .on("data", function(transaction){ |
| 110 | + }); |
| 111 | +
|
| 112 | + // unsubscribes the subscription |
| 113 | + subscription.unsubscribe(function(error, success){ |
| 114 | + if(success) |
| 115 | + console.log('Successfully unsubscribed!'); |
| 116 | + }); |
| 117 | +
|
| 118 | +
|
| 119 | +------------------------------------------------------------------------------ |
| 120 | + |
| 121 | +web3.eth.subscribe('newBlockHeaders') |
| 122 | +===================== |
| 123 | + |
| 124 | +.. code-block:: javascript |
| 125 | +
|
| 126 | + web3.eth.subscribe('newBlockHeaders' [, callback]); |
| 127 | +
|
| 128 | +Subscribes to incoming block headers. This can be used as timer to check for changes on the blockchain. |
| 129 | + |
| 130 | +---------- |
| 131 | +Parameters |
| 132 | +---------- |
| 133 | + |
| 134 | +1. ``String`` - ``"newBlockHeaders"``, the type of the subscription. |
| 135 | +2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. Will be called for each incoming subscription. |
| 136 | + |
| 137 | +------- |
| 138 | +Returns |
| 139 | +------- |
| 140 | + |
| 141 | +``EventEmitter``: An :ref:`subscription instance <eth-subscription-return>` as an event emitter with the following events: |
| 142 | + |
| 143 | +- ``"data"`` returns ``Object``: Fires on each incoming block header. |
| 144 | +- ``"error"`` returns ``Object``: Fires when an error in the subscription occours. |
| 145 | + |
| 146 | +The structure of a returned block header is as follows: |
| 147 | + |
| 148 | + - ``Number`` - **number**: The block number. ``null`` when its pending block. |
| 149 | + - ``String`` 32 Bytes - **hash**: Hash of the block. ``null`` when its pending block. |
| 150 | + - ``String`` 32 Bytes - **parentHash**: Hash of the parent block. |
| 151 | + - ``String`` 8 Bytes - **nonce**: Hash of the generated proof-of-work. ``null`` when its pending block. |
| 152 | + - ``String`` 32 Bytes - **sha3Uncles**: SHA3 of the uncles data in the block. |
| 153 | + - ``String`` 256 Bytes - **logsBloom**: The bloom filter for the logs of the block. ``null`` when its pending block. |
| 154 | + - ``String`` 32 Bytes - **transactionsRoot**: The root of the transaction trie of the block |
| 155 | + - ``String`` 32 Bytes - **stateRoot**: The root of the final state trie of the block. |
| 156 | + - ``String`` 32 Bytes - **receiptRoot**: The root of the receipts. |
| 157 | + - ``String`` - **miner**: The address of the beneficiary to whom the mining rewards were given. |
| 158 | + - ``String`` - **extraData**: The "extra data" field of this block. |
| 159 | + - ``Number`` - **gasLimit**: The maximum gas allowed in this block. |
| 160 | + - ``Number`` - **gasUsed**: The total used gas by all transactions in this block. |
| 161 | + - ``Number`` - **timestamp**: The unix timestamp for when the block was collated. |
| 162 | + |
| 163 | +---------------- |
| 164 | +Callback returns |
| 165 | +---------------- |
| 166 | + |
| 167 | +1. ``Object|Null`` - First parameter is an error object if the subscription failed. |
| 168 | +2. ``Object`` - The block header object like above. |
| 169 | + |
| 170 | +------- |
| 171 | +Example |
| 172 | +------- |
| 173 | + |
| 174 | + |
| 175 | +.. code-block:: javascript |
| 176 | +
|
| 177 | + var subscription = web3.eth.subscribe('newBlockHeaders', function(error, result){ |
| 178 | + if (!error) |
| 179 | + console.log(blockHeader); |
| 180 | + }) |
| 181 | + .on("data", function(blockHeader){ |
| 182 | + }); |
| 183 | +
|
| 184 | + // unsubscribes the subscription |
| 185 | + subscription.unsubscribe(function(error, success){ |
| 186 | + if(success) |
| 187 | + console.log('Successfully unsubscribed!'); |
| 188 | + }); |
| 189 | +
|
| 190 | +------------------------------------------------------------------------------ |
| 191 | + |
| 192 | + |
| 193 | +web3.eth.subscribe('syncing') |
| 194 | +===================== |
| 195 | + |
| 196 | +.. code-block:: javascript |
| 197 | +
|
| 198 | + web3.eth.subscribe('syncing' [, callback]); |
| 199 | +
|
| 200 | +Subscribe to syncing events. This will return an object when the node is syncing and when its finished syncing will return ``FALSE``. |
| 201 | + |
| 202 | +---------- |
| 203 | +Parameters |
| 204 | +---------- |
| 205 | + |
| 206 | +1. ``String`` - ``"syncing"``, the type of the subscription. |
| 207 | +2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. Will be called for each incoming subscription. |
| 208 | + |
| 209 | +------- |
| 210 | +Returns |
| 211 | +------- |
| 212 | + |
| 213 | +``EventEmitter``: An :ref:`subscription instance <eth-subscription-return>` as an event emitter with the following events: |
| 214 | + |
| 215 | +- ``"data"`` returns ``Object``: Fires on each incoming sync object as argument. |
| 216 | +- ``"changed"`` returns ``Object``: Fires when the synchronisation is started with ``TRUE`` and when finsihed with ``FALSE``. |
| 217 | +- ``"error"`` returns ``Object``: Fires when an error in the subscription occours. |
| 218 | + |
| 219 | +For the structure of a returned event ``Object`` see :ref:`web3.eth.isSyncing return values <eth-issyncing-return>`. |
| 220 | + |
| 221 | +---------------- |
| 222 | +Callback returns |
| 223 | +---------------- |
| 224 | + |
| 225 | +1. ``Object|Null`` - First parameter is an error object if the subscription failed. |
| 226 | +2. ``Object|Boolean`` - The syncing object, when started it will return ``TRUE`` once or when finished it will return `FALSE` once. |
| 227 | + |
| 228 | +------- |
| 229 | +Example |
| 230 | +------- |
| 231 | + |
| 232 | + |
| 233 | +.. code-block:: javascript |
| 234 | +
|
| 235 | + var subscription = web3.eth.subscribe('syncing', function(error, sync){ |
| 236 | + if (!error) |
| 237 | + console.log(sync); |
| 238 | + }) |
| 239 | + .on("data", function(sync){ |
| 240 | + // show some syncing stats |
| 241 | + }) |
| 242 | + .on("changed", function(isSyncing){ |
| 243 | + if(isSyncing) { |
| 244 | + // stop app operation |
| 245 | + } else { |
| 246 | + // regain app operation |
| 247 | + } |
| 248 | + }); |
| 249 | +
|
| 250 | + // unsubscribes the subscription |
| 251 | + subscription.unsubscribe(function(error, success){ |
| 252 | + if(success) |
| 253 | + console.log('Successfully unsubscribed!'); |
| 254 | + }); |
| 255 | +
|
| 256 | +------------------------------------------------------------------------------ |
| 257 | + |
| 258 | + |
| 259 | +web3.eth.subscribe('logs') |
| 260 | +===================== |
| 261 | + |
| 262 | +.. code-block:: javascript |
| 263 | +
|
| 264 | + web3.eth.subscribe('logs', options [, callback]); |
| 265 | +
|
| 266 | +Subscribes to incoming logs, filtered by the given options. |
| 267 | + |
| 268 | +---------- |
| 269 | +Parameters |
| 270 | +---------- |
| 271 | + |
| 272 | +1. ``String`` - ``"logs"``, the type of the subscription. |
| 273 | +2. ``Object`` - The subscription options |
| 274 | + - ``Number`` - **fromBlock**: The number of the earliest block. By default ``null``. |
| 275 | + - ``String`` - **address**: An address or a list of addresses to only get logs from particular account(s). |
| 276 | + - ``Array`` - **topics**: An array of values which must each appear in the log entries. The order is important, if you want to leave topics out use ``null``, e.g. ``[null, '0x00...']``. You can also pass another array for each topic with options for that topic e.g. ``[null, ['option1', 'option2']]`` |
| 277 | +3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. Will be called for each incoming subscription. |
| 278 | + |
| 279 | +------- |
| 280 | +Returns |
| 281 | +------- |
| 282 | + |
| 283 | +``EventEmitter``: An :ref:`subscription instance <eth-subscription-return>` as an event emitter with the following events: |
| 284 | + |
| 285 | +- ``"data"`` returns ``Object``: Fires on each incoming log with the log object as argument. |
| 286 | +- ``"changed"`` returns ``Object``: Fires on each log which was removed from the blockchain. The log will have the additional property ``"removed: true"``. |
| 287 | +- ``"error"`` returns ``Object``: Fires when an error in the subscription occours. |
| 288 | + |
| 289 | +For the structure of a returned event ``Object`` see :ref:`web3.eth.getPastEvents return values <eth-getpastlogs-return>`. |
| 290 | + |
| 291 | +---------------- |
| 292 | +Callback returns |
| 293 | +---------------- |
| 294 | + |
| 295 | +1. ``Object|Null`` - First parameter is an error object if the subscription failed. |
| 296 | +2. ``Object`` - The log object like in :ref:`web3.eth.getPastEvents return values <eth-getpastlogs-return>`. |
| 297 | + |
| 298 | +------- |
| 299 | +Example |
| 300 | +------- |
| 301 | + |
| 302 | + |
| 303 | +.. code-block:: javascript |
| 304 | +
|
| 305 | + var subscription = web3.eth.subscribe('logs', { |
| 306 | + address: '0x123456..', |
| 307 | + topics: ['0x12345...'] |
| 308 | + }, function(error, result){ |
| 309 | + if (!error) |
| 310 | + console.log(log); |
| 311 | + }) |
| 312 | + .on("data", function(log){ |
| 313 | + }) |
| 314 | + .on("changed", function(log){ |
| 315 | + }); |
| 316 | +
|
| 317 | + // unsubscribes the subscription |
| 318 | + subscription.unsubscribe(function(error, success){ |
| 319 | + if(success) |
| 320 | + console.log('Successfully unsubscribed!'); |
| 321 | + }); |
0 commit comments