Cocoa: Three ways - first, add to the mash. The mashing process will extract the flavors nicely and give a "chocolatey" flavor. Second - add to the boil with 5-10 minutes remaining. Full disclosure - I haven't used this method. Third, add either cocoa nibs, powder, or chocolate syrup to secondary. Again, I haven't tried this one, I like mashing the cocoa powder so much.
Coffee: Haven't done it but when I do (it's inevitable I'll do a coffee stout at some point), I'll cold-brew my coffee, then test the proportion to add. You can add hot-brewed coffee or coffee beans/powder - I'd generally say secondary would be the place and you want to test to make sure the flavor is where you want it. I did chicory using a tea and got a nice, balanced flavor through trial and error.
On spices/flavor additions in general: Be sure to test. Invest in some basic measuring equipment - a pipette and a graduated cylinder - and test your additions. Teas, tinctures or other solutions of the flavor/spice are a great way to do this. And generally, if you're adding stuff directly to the beer, undershoot. It's a lot easier to add more than to take some out!