It turns out that logging into Facebook to put a heart emoji under a photo of your best friend's new baby may actually be good for you — provided that you also actually follow through in the real world, perhaps by buying a coffee for that frazzled new mom.

New research shows that a moderate use of social media is linked with living longer, if that use helps strengthen real-world connections.

"Interacting online seems to be healthy when the online activity is moderate and complements interactions offline," study author William Hobbs, a postdoctoral fellow at Northeastern University in Boston, said in a statement. "It is only on the extreme end, spending a lot of time online with little evidence of being connected to people otherwise, that we see a negative association" between Facebook use and mortality, Hobbs said.

In the study, Hobbs and his colleagues used a computer program to match the name and birthdate on the Facebook profiles of 12 million people living in California with public records such as birth and death certificates from that state. The study participants were all born between 1945 and 1989. After the researchers made those matches, they removed the names of the people in the study group to protect their privacy, according to the findings published Oct. 31 in the journal Proceedings of the National Academy of Sciences.

An algorithm tallied up how many times, over a six-month period, people engaged with others online. Then, the researchers determined whether the participants were still alive, or had died by the end of the study period. They took into account the participants' age, gender and other factors that could influence their likelihood of dying during the study period.

The team found that people who used Facebook more often tended to live longer than those who didn't. Of course, it is possible that some common factor (such as poor health or economic status) that the researchers did not take into account made people less likely to use Facebook, and that such a factor also made them more likely to die during the study period, the authors noted.

The researchers also found that those people whose social networks were average or slightly larger than average were less likely to die during the study period than those who had the fewest "friends," the researchers found.

People who shared more photos — meaning that they were potentially doing more social activities — also had lower odds of dying during the study, the researchers found.

But the researchers also looked at the nature of the participants' online posts, and here the relationship was more complicated. Some types of posts, such as putting up a photo, implied that the participant had real-life interactions with others, whereas other posts, such as adding comments to other people's posts, did not imply that any real-life interactions took place. People who had either a low level, or a high level of online-only activity had higher rates of mortality than those whose posts suggested more balance between their online-only interactions and their real-life interactions.

"Happily, for almost all Facebook users, what we found is balanced use and a lower risk of mortality," said study co-author James Fowler, a professor of political science in the UC San Diego Division of Social Sciences and of global public health in the UC San Diego School of Medicine.

It's possible that the findings suggest that being popular may improve longevity, the researchers said. But it's also possible that people are more drawn to others who are healthier or have greater odds of living longer, they added. Either way, the connection between strong social ties and better health has been well-established in hundreds of studies, the researchers noted.

"Social relationships seem to be as predictive of lifespan as smoking, and more predictive than obesity and physical inactivity," they wrote. "We're adding to that conversation by showing that online relationships are associated with longevity, too."

Original article on Live Science.