The US law has changed from first-to-invent to first-to-file, so it's best to file a patent as soon as possible.
You can first file a provisional patent application, which won't become a granted patent but will establish a priority date so that your competitor cannot patent your idea later. However later on you will need to file a non-provisional patent application, at which point you hopefully monetize your invention enough to afford a real patent attorney.
If you are certain you can't afford the cost of getting a real patent but want to be on the defensive so that your competitor won't patent your invention and then sue you, you can publish a public technical disclosure anywhere to establish a disclosure date. After that point no one can claim your invention, including yourself.