The Iranian president's visit to Turkey this week comes amid deeply strained bilateral tensions as a result of regional rivalries. However, Israel's assault on Gaza stands to alleviate those problems as Tehran and Ankara find common ground in condemning Israel, with Turkey also seeking to position itself as a mediator in the conflict.
Turkish President Recep Tayyip Erdogan's withering verbal attacks on Israel have put him on the same page as his Iranian counterpart Ebrahim Raisi, who is due to visit Turkey on Tuesday.
But Ankara is also concerned about contagion as the Gaza war draws in countries such as Iran and threatens to destabilise the region.
"Turkey does not approve of demonising Iran. It can still have a dialog, and Turkey has the capacity to cooperate with Iran," says Bilgehan Alagoz, of the Centre for Iranian Studies, a Turkish thinktank.
Alagoz suggests Erdogan is well placed in his talks with Raisi to keep Iran out of the war: "Turkey also has a dialog with Israel. While there are problems with Israel, we still can talk with Israel and with others, including the United States and European countries."
Keeping Iran and its proxies out of the conflict is a priority for Israel and Turkey's Western allies.
Iran has a major role in arming Hamas, says Gallia Lindenstrauss, an analyst with the Institute for National Security Studies in Tel Aviv.
"It is difficult for Israel to concentrate on the Gaza front because the northern front in Israel, the front with Lebanon, is very also very tense," Lindenstrauss says, adding that, "of course, the northern front is all orchestrated by Iran".
Raisi in Turkey
Erdogan's talks with Raisi aren't expected to be confined to the war in Gaza. Until the Hamas attack on Israel on 7 October, Turkish-Iranian tensions had been rising amid growing regional rivalry, in particular in the Caucasus.
Tehran is alarmed at the military successes of Azerbaijan's forces against ethnic Armenians in the disputed Nagorno-Karabakh enclave.
Iran is a strong ally of Armenia and sees Azerbaijan as an increasing threat to its regional influence. However, much to Iran's annoyance, Turkey strongly backs Azerbaijan with a military alliance.
"The recent rhetoric adopted by Iran towards Turkey has created some sensitivity, especially regarding the South Caucasus," says Alagoz.
"Since the beginning of Azerbaijan's liberation activities for the occupied territory in the South Caucasus, Iran has started to adopt anti-Turkey rhetoric."
Battle of influence
Analysts also suggest that Tehran is worried Ankara is seeking to limit Iran's influence across the region. These fears have been stoked by deepening cooperation between Azerbaijan and Israel.
Turkey's expanding its influence into central Asia is adding to Iran's concerns.
"Turkey's connection to central Asia is still a very serious problem because it's going to increase Turkey's influence here in the region, which Iran is not happy with," says Ilhan Uzgel, a political analyst with the Turkish news portal Kisa Dalga.
Tehran's advancing nuclear energy programme is also fuelling bilateral tensions. Turkey is worried that Iran is moving closer to developing a nuclear bomb and triggering a regional nuclear arms race.
But at least for now, the Middle East war and fears that it will spread are bringing the two countries closer.