Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Firebase Emulators support #26

Closed
wants to merge 0 commits into from
Closed

Conversation

Goku-kun
Copy link
Contributor

Hi, @ccrsxx!

Thank you for your patience. This PR resolves #22.

This PR adds support for Firebase local emulators for local development of this Twitter Clone. Additionally, I've also added ample documentation related to the setup of the emulators with images.

One small change that I've add to make is in the src/lib/context/auth-context.tsx file where the clone throws an error in case a profile photo is not provided when using the emulator to login as a new user. This change just adds in a default photorURL for users who don't initially have a photoURL.

Kindly test it out and provide feedback.

@vercel
Copy link

vercel bot commented Aug 14, 2023

Someone is attempting to deploy a commit to a Personal Account owned by @ccrsxx on Vercel.

@ccrsxx first needs to authorize it.

@vercel
Copy link

vercel bot commented Aug 15, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
twitter-clone ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 27, 2023 8:33am

@ccrsxx
Copy link
Owner

ccrsxx commented Aug 15, 2023

Alright cool, I'll try and review it on Sunday. Thanks hehe.

@ccrsxx ccrsxx added documentation Improvements or additions to documentation enhancement New feature or request labels Aug 15, 2023
@ccrsxx ccrsxx self-requested a review August 15, 2023 09:18
@ccrsxx
Copy link
Owner

ccrsxx commented Aug 20, 2023

image

I got this error when trying the emulator. I followed the steps readme.

It seems that the emulator doesn't run when you run the project with the npm run dev:emulators. I even tried to run without the emulator and got the same error as above.

https://github.com/Goku-kun/twitter-clone/blob/main/package.json#L7. I see that dev:emulators run two things, the next dev server and the emulators. The emulator doesn't actually run because the next dev server keeps on running continuously to compile the project.

@Goku-kun
Copy link
Contributor Author

Goku-kun commented Aug 20, 2023

Hi! Thanks for getting back.

I see. It seems we might need to use another package called concurrently to run two scripts at the same time instead of one after another. I'm using a GNU/Linux distro so the & operator functions for me but concurrently works universally. It would be better to use that just in case if someone wants to run this project locally on Windows and other OS where the & is not supported as well.

I can add it and create a commit. Hang on.

@Goku-kun
Copy link
Contributor Author

Hey, @ccrsxx! Can you try again now?

@ccrsxx
Copy link
Owner

ccrsxx commented Aug 20, 2023

image

Seems that I need Java to run the emulators.

@Goku-kun
Copy link
Contributor Author

Goku-kun commented Aug 20, 2023

Yeah, that is correct. Oops, I think I forgot to add that in the README as well. Since I already had it installed, I didn't get this prompt.

Edit: You can use this link to install Java JDK of version 11 or higher.

@ccrsxx
Copy link
Owner

ccrsxx commented Aug 20, 2023

Okay, thanks. It's almost 200 MB, I'm outside with mobile data right now, will install it when I'm at home.

@ccrsxx
Copy link
Owner

ccrsxx commented Aug 21, 2023

@Goku-kun Sorry, couldn't review it right now. I'm pretty busy with work and uni this week.
I'll review it sometime this week if I have free time, if not on Sunday at the latest.

@Goku-kun
Copy link
Contributor Author

Of course, @ccrsxx! No pressure at all. Take your time and do it when you can! All the best with your work and uni though!

Copy link
Owner

@ccrsxx ccrsxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the details in the comment. Hmm, it seems my review the other day was not sent to you.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Goku-kun I just tried it again. I got the emulators working. But right after I logged in with the auth emulator I got this error:

image

It seems that the request from this line in auth-context to firestore emulator failed. It returns 401 (Unauthorized) for some reason.

Tried googling myself, but couldn't find the solution lol. Do you have any idea what might've caused it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to take a look at this tomorrow. We're very close.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might require some active debugging because it doesn't pinpoint any specific issue. I've sent you a request on Discord. My username is Goku-kun. We can meet for a 30-minute call to discuss this and debug it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested it on your end? and it doesn't work?

I should probably be available on Sunday. I will try to test it on my Mac to see if it's specific to Windows only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works for me.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested it on Mac and it works. So I tried troubleshooting it on Windows. Turned out the reason why it doesn't work is that the port that the Firebase emulator uses for Firestore is 8080 already used for qBittorent on my PC, that's why.

What a silly oversight by me. Also, the fact that the emulator not showing any error doesn't help lol.

Copy link
Owner

@ccrsxx ccrsxx Sep 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me do some checking to test if all the features are working, then I'll merge it. Sorry, it took me so long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha. The bug has bitten me differently while running the dev server for NextJS in the project because it's recommended that a port after 1200 be used to run new processes since the earlier ports are reserved for system processes. Port 80 was never unoccupied on Linux. So, I always have to modify the port in the package.json in this project while developing and revert it before committing the changes.

But, you can actually see in the terminal which emulators are running and which emulators have failed to run. I'm glad we were able to pin it down though. LOL!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my end, there's no error when running the emulators even if one of the ports is already used. As you can see on the logs below:

image

That's why I never figured it out until I tested it on Mac and focused on troubleshooting the problem on Windows lol.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good info I don't realize that Linux has that problem, I use both Mac and Windows, neither have that problem. I use port 80 because it looks better for the URL when developing hehe. I might change the port to default in the future so that the linux user doesn't need to manually change the port.

About the PR, I think I could make the steps to run the emulator much shorter by defining the config for the emulator on the project. I'll merge it after I make the change.

@ccrsxx ccrsxx self-assigned this Sep 10, 2023
@ccrsxx
Copy link
Owner

ccrsxx commented Sep 11, 2023

@Goku-kun BTW one problem I noticed. So after I run the emulators, when I want to use the Firebase as the server like the usual, not the local emulator it still tries to use the emulators:

image

I see that on the code it still detects and uses the previously running Firebase apps, which is the emulator. Do you know how to fix this so that when you run the project without the emulator, it runs through the server, not the emulator? I'm thinking of using the env variable to decide whether to use the emulator or Firebase server, but this seems a pretty hacky solution.

@Goku-kun
Copy link
Contributor Author

Goku-kun commented Sep 11, 2023

@Goku-kun BTW one problem I noticed. So after I run the emulators, when I want to use the Firebase as the server like the usual, not the local emulator it still tries to use the emulators:

image

I see that on the code it still detects and uses the previously running Firebase apps, which is the emulator. Do you know how to fix this so that when you run the project without the emulator, it runs through the server, not the emulator? I'm thinking of using the env variable to decide whether to use the emulator or Firebase server, but this seems a pretty hacky solution.

The emulators are triggered based on the development environment condition. The next dev script starts the server in the development environment. This automatically sets the NODE_ENV environment variables to development which is what triggers the emulators.

If we want to use the production Firebase (not the emulator), then we can build the project using npm run build and run the server using npm run start. However, we'll need to enter the same environment variables available in .env.development by setting them in .env.production.

@ccrsxx
Copy link
Owner

ccrsxx commented Sep 12, 2023

Yes, I know that NODE_ENV has a different value whether it's on dev or prod.

What I mean is that I have a separate Firebase project specifically for the dev environment, not the prod one, and I want to run like the dev server without building it. Is it possible to detect running emulators on dev and then only initialize the Firebase emulator if it's running?

I assume many people develop this way (having a Firebase project on the dev) based on this project's Readme, and this change would cause a problem if people try to switch between the emulator and using the Firebase server.

@Goku-kun
Copy link
Contributor Author

Yes, I know that NODE_ENV has a different value whether it's on dev or prod.

What I mean is that I have a separate Firebase project specifically for the dev environment, not the prod one, and I want to run like the dev server without building it. Is it possible to detect running emulators on dev and then only initialize the Firebase emulator if it's running?

I assume many people develop this way (having a Firebase project on the dev) based on this project's Readme, and this change would cause a problem if people try to switch between the emulator and using the Firebase server.

That is true. I suppose we can create an emulator-only environment variable that triggers the emulators while running the same next dev script.

@ccrsxx
Copy link
Owner

ccrsxx commented Sep 16, 2023

Alright, it seems there's no way to detect emulators besides using the env variable.

I will make env specifically for the dev emulator, by default it should be false.

@ccrsxx
Copy link
Owner

ccrsxx commented Sep 17, 2023

Bruh Github is weird. I just pushed the env update. This is the git log history:
image

But it automatically closes it and reverts to the previous pr commit. I'll try to fix it.

@ccrsxx
Copy link
Owner

ccrsxx commented Sep 17, 2023

@Goku-kun Damn, what the heck did I do lol. I screwed up the pr, I can't push the above commits because the pr is closed. I don't close it, and I can't reopen it now.

Can you push your local repo to your fork repo again, that will hopefully bring this PR back. I was close to merging it lol.

@ccrsxx
Copy link
Owner

ccrsxx commented Sep 17, 2023

I make another branch here: https://github.com/ccrsxx/twitter-clone/tree/Goku-kun-main. All the changes are intact from this pr.
But yeah, try to restore this pr first if possible.

@Goku-kun
Copy link
Contributor Author

Goku-kun commented Sep 19, 2023

Hmm. I'm not sure if GitHub allows re-opening of PRs the same way it does for issues.

Edit: I tried doing what you suggested above. That didn't re-open this PR. Might as well shift to a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Enabling Local Emulator for Firebase for local development
2 participants