I used to use metamask for Dapp development..Everything works fine, but trust wallet(Dapp mobile browser) doesn't emit events comes of from contract..My Dapp is heavily rely on events..if they don't emit, it will not worth for the Dapp.. I saw a question in github repo, someone mentioned that you should use your own custom provider for events..but whenever I search how to use your custom provider on google, it comes up with use metamask or something else..

I'm using Pure vanilla js, So I don't think I need to use truffle...However, I'm using Web3 CDN for adding it to my Dapp..now the basic code is this:

window.addEventListener('load', async() => {
if (window.ethereum) {
    const web3 = new Web3("ws://localhost:8545");
    // const web3 = new Web3(window.ethereum);
    try {
        await window.ethereum.enable();
        return web3;
    } catch (error) {
} else if (window.web3) {
    const web3 = window.web3;
    return web3;
} else {
        "Non-Ethereum browser detected. You should consider trying MetaMask!"

Now, my main question is how to use custom provider for Dapp so I can emit events effectively without have to relying on third party providers..however, i'll still rely on them for signing the transactions..

Now the above code gives an error:

WebSocket connection to 'ws://localhost:8545/' failed: Error in connection establishment: 
a @ browser.js:24
s.connect @ index.js:90. 
s @ index.js:77
c.setProvider @ index.js:69
e @ index.js:40
packageInit @ index.js:45
 h @ index.js:39
(anonymous) @ auth2.js:46

Kindly assist me to solve this issue.. Ps: I don't mean to create my own provider from scratch..Moreover, I don't know the process either..

1 Answer 1


With const web3 = new Web3("ws://localhost:8545"); you are trying to connect to a local provider. For this you must set up your own node using for exemple the Geth or Parity clients.

However, running its own node is very expensive on the mainnet. You could instead use Infura which is a blockchain provider, that means they provide public remote nodes you can rely on. Infura supports websocket so you should be able to manage events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.