As per https://web3js.readthedocs.io/en/1.0/web3.html#value the HttpProvider is deprecated. Using the web3 beta 1.0.
It seems funny to me that it would be being deprecated. But I am curious as to how I would transition to something else like websockets in the future using a service like Infura.
My current code is something like:
import Web3 from 'web3';
let web3;
if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined'){
//We are in the browser and Metamask is running
web3 = new Web3(window.web3.currentProvider);
} else {
//We are on the server *OR* the user is not running Metamask
const provider = new Web3.providers.HttpProvider(
'https://rinkeby.infura.io/v3/API_KEY'
);
web3 = new Web3(provider);
}
export default web3;
Edit: From @Ismael how to currently implement the Infura Websocket provider Infura web3 provider for Events (.get & .watch) see answer by Maheshmurthy. As far as I know it's not production ready and doesn't require an API key yet : https://github.com/INFURA/infura/issues/97 .
Maybe a better question is: Is there a short explanation about why httpProviders are deprecated other than: no subscriptions and they are better. And for a beginner developing with web3 -- If I just switch providers to wss what differences can I expect? Infura specific not necessary.