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

Persistent Connection / Keep-Alive Requests #1489

Closed
matthewwoop opened this issue Mar 23, 2017 · 2 comments
Closed

Persistent Connection / Keep-Alive Requests #1489

matthewwoop opened this issue Mar 23, 2017 · 2 comments

Comments

@matthewwoop
Copy link

Any recommendations on how to achieve a persistent connection between a Next app and an API.

Ideally, open the connection on componentWillMount and close the connection on componentWillUnmount

I have only found solutions that work either on the client (e.g. XMLHttpRequest) or the server (e.g. axios httpAgent), but not universally

@matthewwoop
Copy link
Author

Found a semi-solution.

In componentDidMount, one has access to the window object, which in turn exposes XMLHttpRequest.

This allows for something like the following

componentDidMount() {
        this.liveClient = new window.XMLHttpRequest();
        this.liveClient.open('get', url);
        this.liveClient.send();
        this.liveClient.onprogress = function(){
            //do something
        };
}

Going to leave this open for a bit in case any one has better suggestions, or sees pitfalls in this method. Ideally would still like to initiate the connection on componentWillMount, but this will do for now.

@arunoda
Copy link
Contributor

arunoda commented Mar 24, 2017

Do not use componentWillMount. It runs on the server too.
You need to do this on the client, so use componentDidMount to start the connection.
Then use componentWillUnmount to close the connection.

@arunoda arunoda closed this as completed Mar 24, 2017
@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants