Hi using MessageData is correct, however the best way is to call ExecuteOnMainThread as you can see in the websocket example I share with you here.
This lambda will be executed on main thread, therefor it will be safe to create object and perform undo from it. One note make sure to pass a copy of the data to your lambda, since this lambda will be executed when the main thread is free and you do not know exactly when it can happen. And it can be latter than your OnMessage function, so with a copy you are sure that object are not nullptr.
Cheers,
Maxime.