Melt the butter in a skillet over medium heat.
Add the sliced onions, sugar, salt, and pepper. Cook for about 15-20 minutes, stirring occasionally, until the onions are caramelized and golden brown.
Pour in the broth and cook for an additional 5 minutes, allowing the liquid to reduce slightly. Remove from heat and stir in the thyme if using.
Heat a separate skillet over medium heat. Brush one side of each slice of bread with olive oil.
On the unbrushed side, layer half of the Gruyère cheese, followed by a generous spoonful of the caramelized onion mixture, then the mozzarella cheese, and finally the remaining Gruyère cheese. Top with another slice of bread, brushed side up.
Place the sandwich in the heated skillet and cook for 3-4 minutes until golden brown.
Carefully flip the sandwich and cook for another 3-4 minutes on the other side, pressing down gently with a spatula.
Remove from the skillet, slice in half, and serve warm.