-
-
Save zenithlight/5170d5621648f0b04a7aac5e3992a556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# OPTION 1 ======================================================================================== | |
client = Etherpc::WebSocketClient.new "wss://mainnet.infura.io/ws/v3/#{PROJECT_ID}" | |
subscription_request = Etherpc::Subscriptions::NewHeads.new | |
subscription = client.call subscription_request | |
number_of_messages = 0 | |
subscription.on_event do |event| | |
puts "New block at height #{event.number}" | |
number_of_messages += 1 | |
if number_of_messages == 10 | |
subscription.cancel | |
end | |
end | |
# OPTION 2 ======================================================================================== | |
client = Etherpc::WebSocketClient.new "wss://mainnet.infura.io/ws/v3/#{PROJECT_ID}" | |
subscription_request = Etherpc::Subscriptions::NewHeads.new | |
number_of_messages = 0 | |
client.subscribe new_heads_subscription do |event, cancel| | |
puts "New block at height #{event.number}" | |
number_of_messages += 1 | |
if number_of_messages == 10 | |
cancel | |
end | |
end | |
# OPTION 3 ======================================================================================== | |
client = Etherpc::WebSocketClient.new "wss://mainnet.infura.io/ws/v3/#{PROJECT_ID}" | |
subscription_request = Etherpc::Subscriptions::NewHeads.new | |
subscription = client.call subscription_request | |
number_of_messages = 0 | |
while subscription.active | |
puts "New block at height #{subscription.receive.number}" | |
number_of_messages += 1 | |
if number_of_messages == 10 | |
subscription.cancel | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment