How to restore transport, producer, consumer by id?

From this code will retrieve transport object :

const transport = await router.createWebRtcTransport()

I want to save transport ID, producer ID, consumer ID into database like MySQL, Postgre, etc. So if I save the ID, how to restore them? I don’t know how to re-initialize ID to sepesific object type.

There is nothing in-built and I think there will never be as this is not desireable.

To get the relevant object you can save the mappings for relevant objects with key value pairs where key will be the id of transport and value will be the full transport object and then use this mapping to get your desired object whenever you need. These objects are only available at runtime once the server is turned off or restarted all is lost and everything is created from fresh.

var transports = {‘transport_1_id’: transport_1_obj, ‘transport_2_id’: transport_2_obj};
var transport_1_obj = transports[transport_1_id];

1 Like

These IDs are always generated by the running mediasoup library when it creates new objects, at least those of transports and consumers. Once the transports are closed, the IDs are not valid anymore. It is just useless to save them to a database.

1 Like

So we just put in some variable like an array because currently I also place it in a javascript map, so I’m thinking about scalability when using memory/caching

These objects will remain in variables till the server is alive and there is no way to store/restore them in/from cache. You just keep these objects in variable and then keep track of which object is on which server then if you need to access some object which is not in current server but on another server you just signal the request to the corresponding server to perform the relevant action on that object. That’s how you will be doing it.

So to summarise keep these objects in mapping and keep track of which transport etc is on which server and then ping that relevant server to perform some action. The rest depends upon how you do it.

1 Like