Feeling lonely stems from a lack of loving attention on yourself. Other people can relieve that by giving you their attention, but you can also do it by yourself.
The key insight is that all attention is the same. It doesn’t matter if it comes from you or someone else. All attention is identical in it’s nature – so get rid of the notion that only other people can help you with loneliness.
Learn to give yourself loving attention. Listen to yourself. Give yourself space to express your feelings. Do what makes you genuinely feel loved.
Be there for yourself and for others. Also, let others be there for you when they offer it.