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

SocketProvider does not receive messages #188

Closed
barrasso opened this issue May 5, 2019 · 5 comments · Fixed by #189
Closed

SocketProvider does not receive messages #188

barrasso opened this issue May 5, 2019 · 5 comments · Fixed by #189

Comments

@barrasso
Copy link
Contributor

barrasso commented May 5, 2019

I have setup the Web3SocketDelegate according to the Usage docs and tests.

I have successfully connected to the socket, however, I am unable to get any sort of messages (even when using different types of filters and subscriptions).

Steps to reproduce:

class MyViewController: UIViewController, Web3SocketDelegate {

    /* Web3Socket Delegate */
    var socketProvider: InfuraWebsocketProvider?

    // Protocol method messages received from WebSocket server
    func received(message: Any) {  // This is never called
        // Do something with message
        print(message as! String)
    }
    
    // Error receieved from socket
    func gotError(error: Error) { // Neither is this
        print(error.localizedDescription)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupViews()
        
        // Connect to Infura socket endpoint
        socketProvider = InfuraWebsocketProvider.connectToInfuraSocket(.Ropsten, delegate: self)
        try! self.socketProvider?.subscribeOnNewPendingTransactions()
    }
}

In the console, I see websocket is connected. But after sending some transactions, I don't receive any socket messages or events.

@barrasso
Copy link
Contributor Author

barrasso commented May 5, 2019

My goal is to check if a certain transaction was mined or confirmed. Is this possible?

If not, I will just check for pending transactions...

@TonioMacaronio
Copy link
Collaborator

@barrasso I'll study this problem in the next few days and fix it. Perhaps you need to set some delay between connecting and subscribing to get rid of it now. Also, I will expand the functionality of the Infura websocket provider

@barrasso
Copy link
Contributor Author

barrasso commented May 6, 2019

@BaldyAsh I tried with a long delay between connecting and subscribing... still no dice. Thanks for the help.

@TonioMacaronio
Copy link
Collaborator

@barrasso found what is wrong. Now Infura websocket subscription requests should include your Infura PROJECT_ID. Web3swift project id will be set by default, but it will be possible to set your own

@barrasso
Copy link
Contributor Author

barrasso commented May 7, 2019

@BaldyAsh Cool now I was able to connect to my own project using the default WebsocketProvider:

let provider = WebsocketProvider.connectToSocket("wss://ropsten.infura.io/ws/v3/MY_PROJ_ID", delegate: self)

However, the default WebsocketProvider class doesn't have access to the convenient methods used by the InfuraWebsocketProvider, such as .filter or .subscribe.

I can do a writeMessage(_ data: Data), but I guess I'll have to do the JSON encoding first like here:

    public func writeMessage(method: InfuraWebsocketMethod, params: [Encodable]) throws {
        let request = JSONRPCRequestFabric.prepareRequest(method, parameters: params)
        let encoder = JSONEncoder()
        let requestData = try encoder.encode(request)
        writeMessage(requestData)
    }

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

Successfully merging a pull request may close this issue.

2 participants