Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen to events #1

Closed
creazy231 opened this issue Jul 25, 2018 · 5 comments
Closed

Listen to events #1

creazy231 opened this issue Jul 25, 2018 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@creazy231
Copy link

Hey there,
Im using the nuxt-box and Im very satisfied with it.
Now I want to listen to events my contract emits but I cant find any solution how to do this.

That's wat I tried without success:

this.$store.state.myContract.events.MyEvent({fromBlock: 0, toBlock: 'latest'}, (err, event) => {
                if (err) console.log(err);
                console.log(event);
            }).on('data', function(event){
                console.log(event);
            }).on('changed', function(event){
                console.log('on changed');
            }).on('error', console.error);

Anyone here could give me a small example?
Thanks a lot :)

@pyyding
Copy link
Contributor

pyyding commented Jul 25, 2018

Thanks for the positive comments!

MetaMask doesn't support web sockets right now but they're getting close. This means we can't watch these events by default yet.

This guy had the same issue:
MetaMask/metamask-extension#2601

For a hacky solution you can check out this thread:
MetaMask/metamask-extension#3642 (comment)

@rahulrumalla
Copy link
Contributor

@creazy231 I did some digging around. The problem is with the connection to web3. The out-of-box solution connects to web3 using json-rpc over http. Since we need listen to event as they're emitted, we need to persist that connection, meaning a websocket endpoint is needed.

This change needs to happen at https://github.com/Paperchain/nuxt-box/blob/master/src/plugins/web3.js

web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// or use infura
web3.setProvider(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/_ws'));

You can also use the local geth ipc, recommended as the most secure way.

// Using the IPC provider in node.js
var net = require('net');
var web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path

I haven't tested the above code out yet. But if you do try it, please update us :) Hope this helps for now.

@rahulrumalla rahulrumalla added the question Further information is requested label Aug 1, 2018
@creazy231
Copy link
Author

@rahulrumalla thanks a lot for your accomplished answer. I successfully manage the events via websockets now doing the following:
For everything except events I register my provider with the following code:
const instance = new Web3(Web3.givenProvider || 'http://localhost:8546');

and to listen to events I created a separate instance with the following code:
const instance = new Web3('ws://localhost:8546');

therewith Im able to listen to events like so:

    this.$store.state.theContract.web3Events.events.MyEvent({}, (error, data) => {
        if (error)
            console.log("Error: " + error);
        else
            console.log("Log data:");
            console.log(data);
    });

@rahulrumalla
Copy link
Contributor

@creazy231 Awesome, stuff! Thanks for giving us the update. :)

@creazy231
Copy link
Author

@rahulrumalla no problem :) just took me half a day to figure out how to listen to events :D the reason why it took me so long was, that there are sooo many code examples out there showing working solutions with the OLD version of web3 and Im supposed to use the newest version

hopefully I can help some people with my solution :)
And of cause your solution would work, too. (well its the same in general with other code) 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants