bei
at / with
Indicates being at a person's place (at home) or at an employer.
Ich war gestern Abend bei Freunden zum Essen.
I was at friends' place for dinner yesterday evening.

at / with / near
Loading your saved state.
at / with
Indicates being at a person's place (at home) or at an employer.
Ich war gestern Abend bei Freunden zum Essen.
I was at friends' place for dinner yesterday evening.

The preposition 'bei' always requires the dative. Make sure to decline the article and pronoun after 'bei' correctly.
Er wohnt noch bei seinen Eltern.
He still lives with his parents.
'Bei' is used to indicate a place in connection with a person (e.g., at their home) or an employer/company. It answers the question 'Wo?' (where?).
Ich treffe mich später bei Paul.
I'm meeting up with Paul later at his place.
To express that two actions happen at the same time, the structure 'beim + nominalisierter Infinitiv' (while + nominalized infinitive) is often used. 'Beim' is the short form of 'bei dem'.
Ich höre gerne Musik beim Joggen.
I like listening to music while jogging.
With 'bei', you can also describe a circumstance or condition. Typical phrases are 'bei gutem Wetter' (in good weather), 'bei Fragen' (if you have questions), or 'bei Bedarf' (if needed).
Bei Fieber sollten Sie einen Arzt aufsuchen.
If you have a fever, you should see a doctor.
The expression 'etwas bei sich haben' means that you are carrying something with you. The pronoun changes according to the person: 'bei mir' (on me / with me), 'bei dir' (on you / with you), 'bei sich' (on oneself / with oneself), etc.
Hast du deinen Ausweis bei dir?
Do you have your ID on you?
if / when
'wenn' and 'bei' can both express a condition or a simultaneous situation. 'wenn' introduces a whole subordinate clause with a verb, for example 'wenn es regnet' (if/when it rains). 'bei' is a preposition and is used with a noun, for example 'bei Regen' (in rain) or 'beim Essen' (while eating). 'bei' often sounds shorter and more factual, especially in fixed or general situations. They are confused because 'bei Regen' roughly means 'wenn es regnet' (if/when it rains). But grammatically, the sentence is built very differently.

Bei starkem Regen bleiben die Kinder in der Pause im Schulhaus.
In heavy rain, the children stay inside the school building during break.
Wenn ihr morgen anruft, kann ich den Termin sofort bestätigen.
If you call tomorrow, I can confirm the appointment right away.