Crabs have an internal set of gills to breathe. They work a little differently than fish, which have an opening behind the gills through which water can be drawn in. Instead, crabs have small appendages resembling paddles that move the water over their gills and out through an anterior tube at their mouth, which is where you will see bubbles.
The bubbling is the process by which carbon dioxide is expelled while oxygen is drawn in, just as we do when we blow bubbles. Since crabs spend most of their lives underwater, with occasional excursions to dry land or intertidal pools, they have gills built for breathing exclusively in water. Those gills need to be wet to function, so when they do make the transition to dry land or intertidal pool, it’s not uncommon to see them “breathe” through their mouths by blowing bubbles.
In most cases, the bubbles are clear and do not indicate that the crab is in distress. However, if the bubbles become brown and smell fishy, it could mean that your crab is dehydrated or needs more water. In most cases, it’s a good idea to simply place your crab back in the tank and provide a source of water appropriate for its habitat.
Foaming at the mouth can also happen if a crab becomes too hot or is stressed. If you are feeding your crab too often, changing its environment frequently or if it has been handled too much, it may be overheated and start to bubble to vent excess heat.