First you need to have web3.js module in your project. You may refer here for guide.
Once you have added web3 you can use the web3 object to interact with an Ethereum network by running an local node or via injected web3 with services like metamask.
Sample code snippet to check for metmask or connect to local node is,
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.log('No web3? You should consider trying MetaMask!')
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
You may refer metamask faq and this post for more details.
Note that to use,
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
you need to have a node running locally with the rpc enabled at the port:8545, you can do it by running the command:
geth --rpc --rpcaddr <ip> --rpcport <portnumber>
with the <ip>
being localhost
and <portnumber>
being 8545
.
Since you want to specifically connect to Rinkeby network, you may refer here to find how to synchronize a full-node with Rinkeby and if using metamask you can select the network from the dropdown available with the plugin-UI.
And with the web3 object initialized, you can use JavaScript Web3 API to interact with the ethereum network as documented here.
Hope this helps!