We study when the period and the index of a class in the Brauer group of the function field of a real algebraic surface coincide. We prove that it is always the case if the surface has no real points (more generally, if the class vanishes in restriction to the real points of the locus where it is well-defined), and give a necessary and sufficient condition for unramified classes. As an application, we show that the u-invariant of the function field of a real algebraic surface is equal to 4, answering questions of Lang and Pfister. Our strategy relies on a new Hodge-theoretic approach to de Jongs period-index theorem on complex surfaces.