China’s leader, Xi Jinping, arrived in Vietnam on Tuesday for a relatively rare trip abroad, seeking to elevate ties with an important neighbor just three months after President Biden visited Hanoi on a similar mission.

Few nations now feature more centrally in the great-power competition between the United States and China, placing Vietnam, which has a long history of fierce independence, in a high-risk, high-reward position. Keeping both giants happy could mean a transformative economic boost; angering one or the other could bring heavy costs.

“This is a very delicate dance for Vietnam’s government,” said Alexander Vuving, a professor at the Asia-Pacific Center for Security Studies in Honolulu. “They have to dance on a very thin tightrope, and the tightrope has become even thinner.”

Mr. Xi, analysts say, wants to test Vietnam’s intentions, seeking reassurances that it is not siding against China with the United States after Washington and Hanoi agreed in September to form a “comprehensive strategic partnership.”

That is usually the highest diplomatic status that Vietnam affords. Beijing reached that level with Hanoi 15 years ago, but Mr. Xi has been pressing Vietnam to do one better by joining what China calls a “community of common destiny.”

He introduced that phrase about a decade ago as part of a plan to line up regional support and tamp down distrust among Southeast Asian nations that were once China’s tributary states. Several countries, including Cambodia, Laos and Myanmar, have signed on, but Vietnam has resisted, fearing that it could be interpreted as acceptance of Chinese hegemony.

In an opinion article published in Vietnamese state media, Mr. Xi said a “community with a shared future” would be significant for the two countries — a hint, perhaps, of phrasing that might serve as a compromise.

Hanoi’s concerns about China’s reach are particularly acute in the South China Sea, where Vietnamese and Chinese ships have clashed repeatedly since the 1970s over competing claims to islands and oil and gas reserves.

Chinese and Vietnamese leaders, including Nguyen Phu Trong, the general secretary of Vietnam’s ruling Communist Party, are expected to discuss the territorial disputes on Tuesday and Wednesday as they seek to produce a joint statement showing diplomatic progress.

The agenda is also likely to include a cargo rail project near the Chinese border that could fall under China’s Belt and Road Initiative, as well as the possibility of working together on rare earth minerals, a topic of great interest for both Washington and Beijing.

Vietnam has the world’s second-largest rare-earth deposits, according to the U.S. Geological Survey. China dominates processing of the minerals, controlling prices for many of the materials needed for smartphones, electric vehicles, high-tech weapons and other crucial products.

Washington sees Vietnam — and other partners with mining expertise, such as Australia — as the potential scaffolding for an alternative supply chain that would displace China’s monopoly.

After Mr. Biden’s visit, the White House announced that the United States would help Vietnam quantify its deposits and would work on technical cooperation with the country, one of several signs that American manufacturers see it as a low-cost, less risky alternative to China.

Several American semiconductor companies also announced major investments in Vietnam.

For Mr. Xi, anything that might complicate or delay U.S. plans for rare earths or microchips could be considered a victory — for the beleaguered Chinese economy and for nationalists at home.

“Xi’s visit is to say that Vietnam has not escaped China’s orbit just yet,” said Huong Le Thu, deputy director of the International Crisis Group’s Asia program. She added that while his trip would include expressions of closeness to Vietnam, he was also “sending a message to the Chinese audience — that Beijing has not ‘lost’ a country in its periphery to the ‘Western camp.’”

Vietnam’s leaders seem to understand China’s need to present an image of strong friendship. In Vietnamese state media, officials have emphasized that this is Mr. Xi’s third trip to Vietnam, noting that no other Chinese leader had made as many visits.

American officials have also been busy, and not just the president. Gen. Charles A. Flynn, the commander of U.S. Army Pacific, has met three times in the past four months with senior Vietnamese officials, including a visit to the Communist Party headquarters in Hanoi.

For Vietnam, which is often praised for its “bamboo diplomacy” (firm roots, flexible branches), the goal is balance. Welcoming China’s leader reminds the United States that Vietnam is not beholden to American entreaties. But with China run by Mr. Xi, its most assertive leader since Mao Zedong, bending too far toward Beijing’s wishes may pose a greater risk, including the potential for domestic backlash.

Anti-China protests broke out across Vietnam in 2014, after Chinese and Vietnamese ships collided near an oil rig in the South China Sea, and again in 2018, after Hanoi said it would allow foreigners to lease land in three special economic zones.

“The Vietnamese leadership is playing it well, thus far, understanding the challenges but also opportunities coming from competing great powers and making the most of Vietnam’s strategic position,” Ms. Huong said. “How sustainable that can be — it’s yet another question.”