I think Ramayan is written after it happened. but i also listen that it was written before it happened.
As per the conversations of Narada & Valmiki and Lord Brahma and Valmiki in Sarga1-2, Balkand, Valmiki Ramayana:
Till Yudhakand and few stories of Uttarakand:
After they happened as narrated by Narada to Valmiki
Later stories of Uttarakand:
Before they happened as per the divine powers given by Lord Brahma to Valmiki
Note: few scholars say that Uttarakand and Balkand were not part of original Valmiki Ramayana and were added later.
This is confirmed by Lord Brahma in Uttarakand too:
Which stories in Uttarakhand are written before they happened:
Narada to Valmiki (Valmiki Ramayana 1.1.94 to 1.1.97)
अश्वमेधशतैरिष्ट्वा तथा बहुसुवर्णकै: ।।।।
गवां कोट्ययुतं दत्वा ब्रह्मलोकं प्रयास्यति ।
असंख्येयं धनं दत्वा ब्राह्मणेभ्यो महायशा: ।।
Highly renowned Rama, having satisfied the gods with the performance of a hundred of aswamedhas and many suvarnakas bestowing hundreds of thousands of cows and immense wealth on the brahmins, will return to his heavenly abode.
राजवंशान्शतगुणान्स्थापयिष्यति राघव: ।
चातुर्वर्ण्यं च लोकेऽस्मिन् स्वे स्वे धर्मे नियोक्ष्यति ।।
Rama will establish hundredfold royal dynasties and employ the four castes to do their respective duties, in this world.
दशवर्षसहस्राणि दशवर्षशतानि च ।
रामो राज्यमुपासित्वा ब्रह्मलोकं प्रयास्यति ।।
Rama, reigning the kingdom for eleven thousand years, will attain to his heavenly abode.
मा निषाद प्रतिष्ठां त्वमगमः शाश्वतीः समाः।
यत् क्रौंचमिथुनादेकमवधीः काममोहितम् ।।
अयं श्लोकः एव रामायणस्य मूलप्रेरकः अपि चाभवत् ।
This curse was given to a Nishad after he had killed the male Kronch bird from a couple of two birds. The rishi was Valmiki... It was the first shlok of Ramayana.
After this incident, Saint Narada visits hermitage of Valmiki. Rishi Valmiki queries about a single perfect individual bestowed with all good qualities enumerated by him. Devrishi Narada, knower of past, present and future, identifies such a man, describes virtues, qualities of Sri Rama , narrates briefly the story of his life. Shlokas can be found here : https://www.valmiki.iitk.ac.in/sloka?field_kanda_tid=1&language=dv&field_sarga_value=1
If you read any one of the shloka in conversation :
न पुत्रमरणं किञ्चिद्द्रक्ष्यन्ति पुरुषा: क्वचित् ।
नार्यश्चाविधवा नित्यं भविष्यन्ति पतिव्रता: ।।1.1.91।।
During the period of Rama's rule, nowhere would men witness the death of their sons or women widowed. They would ever remain chaste and devoted to their husbands.
Here, भविष्यन्ति is the First person(प्रथम पुरुष) plural(बहुवचन ) future tense(लृट्लकार भ्वादिगण परस्मैपदी) ofverb be(भू ).
This ensures that everything that happened was before or contemporary to Rama and certainly not after it.