Live data from Hacker News

How to build undo/redo in a multiplayer environment by Liveblocks

liveblocks.io

31–40 of 49 posts

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#31

Love the interactive visuals. Would be awesome to see how that would work with other use cases at some point. Is that something you’re planning to do?

By "other use cases", are you talking about apps that are not design tools like Notion or Google Spreadsheet? Or other edge cases related to undo/redo?

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#32
I've implemented undo/redo a number of times. I agree whole-heartedly that the Command pattern is the way to do it. Undo has a reputation for being difficult, but my experience is that it's smooth sailing as long as you build it into the tool on day 1. If the app is architected around undo, it's easy, but trying to retrofit it onto an application later is always a nightmare.

This is very similar to the experience of writing a networked multiplayer game. If you build a single-player game and try to bolt multiplayer on later, you're gonna have a bad time. But if you design it for multiplayer initially and treat single-player mode as essentially just a multiplayer game with only one player, it's relatively easy.

I think both of these come down to the same core issue: mutating state.

When playing a game, or editing a document, you are mutating some state. To support undo, you need to capture all of those mutations so that you can reverse them. To support multiplayer, you need to capture them so that they can be synchronized with the other players.

It's trivially easy in most programs to just directly mutate some state by setting fields or by calling methods that do that under the hood. So, if you just start coding, you will end up with mutation happening everywhere. At that point, you have already lost.

But if you design your application for undo, you isolate the document state from the rest of the application so that the only way to modify it is by going through the undo/redo mechanism. (In other words, the only way to apply a change is to create a Command object which does it on your behalf.) Likewise, if you design for multiplayer, you'll build a separation between game state and the rest of the application. Then the program has a well-defined interface that can modify the state.

Once all mutation goes through a narrow well-defined interface, it's relatively easy to grow the application over time without compromising undo or multiplayer.

But if you're adding that afterwards, you have to dig through the program to find every single piece of code that changes some state. It's hell.

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#33

Love the interactive visuals. Would be awesome to see how that would work with other use cases at some point. Is that something you’re planning to do?

By "other use cases", are you talking about apps that are not design tools like Notion or Google Spreadsheet? Or other edge cases related to undo/redo?

Yes, mainly text collaboration.

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#34

Nice to see someone make a product out of Cloudflare Workers (and Durable Objects for sync?). Also, excellent explanation and visualizations :) Good luck with all of it

Yes! We're using Durable Object under the hood :) Thanks to you for providing such a great platform!

Oh, I dont work at cloudflare I'm just a user, have been building workers for about 3 years.

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#35
post #30

As someone who's built this kind of multi-player undo/redo in the past all I can say is this looks amazing and I can't wait to try it in one of my projects - Thanks for building this!

Thank you! Can’t wait to see what you build with it.

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#37

What a fantastic, well-written blog post - well done. Out of curiosity, what's the max # of simultaneous connections per room that LiveBlocks can support? (it's hidden behind the enterprise signup flow today)

We support 20 simultaneous connections per room on the Pro plan. With the organization plan, we’ve been able to increase this to about 50 simultaneous connections per room depending on the use case.

We would technically be able to go beyond that but will likely require bigger servers. Depends on what you’re trying to build.

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#38

I've implemented undo/redo a number of times. I agree whole-heartedly that the Command pattern is the way to do it. Undo has a reputation for being difficult, but my experience is that it's smooth sailing as long as you build it into the tool on day 1 . If the app is architected around undo, it's easy, but trying to retrofit it onto an application later is always a nightmare. This is very similar to the experience of…

This is some great nugget of knowledge, thanks for sharing. it's too bad all my programming enthusiasm is going into a 9-5

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#39
> In a multiplayer command-based undo/redo system, we can also solve [intermediary commands] by pausing and resuming the history stack at the right time.

Suppose that instead of managing the overall state of the history stack, you gave each command a unique ID and allowed commands to be updated/overwritten as they develop? Apart from a few bytes of memory overhead per command, what am I missing?

Re: How to build undo/redo in a multiplayer environment by Liveblocks

#40

I've implemented undo/redo a number of times. I agree whole-heartedly that the Command pattern is the way to do it. Undo has a reputation for being difficult, but my experience is that it's smooth sailing as long as you build it into the tool on day 1 . If the app is architected around undo, it's easy, but trying to retrofit it onto an application later is always a nightmare. This is very similar to the experience of…

Command-pattern is the secret sauce for the ultimate and optimal Undo functionality. Glad you're spreading the good word!
Post reply on HN