Running mediasoup on server with bun

Hi friends, I am facing issue running mediasoup on server using bun.

How to reproduce:
$ bun run mediasoup.ts

import * as mediasoup from "mediasoup";

const main = async () => {
  const mediasoupWorker = await mediasoup.createWorker({
    logLevel: "debug",
    logTags: [],
    rtcMinPort: 60000,
    rtcMaxPort: 60010,
  });
};

main();

output with error:

30 |         super();
31 |         logger.debug('constructor()');
32 |         this.#producerSocket = producerSocket;
33 |         this.#consumerSocket = consumerSocket;
34 |         // Read Channel responses/notifications from the worker.
35 |         this.#consumerSocket.on('data', (buffer) => {
            ^
TypeError: undefined is not an object (evaluating 'this.#consumerSocket.on')
      at new Channel (/www/mediasoup/node_modules/mediasoup/node/lib/Channel.js:35:8)
      at new Worker (/www/mediasoup/node_modules/mediasoup/node/lib/Worker.js:111:24)
      at /www/mediasoup/node_modules/mediasoup/node/lib/index.js:32:19
      at createWorker (/www/mediasoup/node_modules/mediasoup/node/lib/index.js:27:30)
      at /www/mediasoup/mediasoup.ts:4:32
      at main (/www/mediasoup/mediasoup.ts:3:13)
      at /www/mediasoup/mediasoup.ts:12:0
      at processTicksAndRejections (:55:76)

Mediasoup version: 3.12.13
Bun versions: 1.0.3 & (1.0.4 canary) - tested with both
Bun node version: 20.8.0

Appreciate in advance for your help!

mediasoup works on Node. If Bun doesn’t implement all the APIs of Node then mediasoup doesn’t work on Bun.

Please, investigate the missing stuff here and report to Bun project rather than here.

Hi Iñaki,

Thank you a lot for your super fast response.
Maybe you can assist me on which node api’s does mediasoup rely in current example, so I can interact with bun developers and help them to port it and make mediasoup available using bun as well?

Does it rely on Node::net?

Thank you in advance.

So finally I had to investigate it.

Ok, in Worker.ts we create an instance of Channel:

		this.#child = spawn(
			// command
			spawnBin,
			// args
			spawnArgs,
			// options
			{
				env :
				{
					MEDIASOUP_VERSION : '__MEDIASOUP_VERSION__',
					// Let the worker process inherit all environment variables, useful
					// if a custom and not in the path GCC is used so the user can set
					// LD_LIBRARY_PATH environment variable for runtime.
					...process.env
				},

				detached : false,

				// fd 0 (stdin)   : Just ignore it.
				// fd 1 (stdout)  : Pipe it for 3rd libraries that log their own stuff.
				// fd 2 (stderr)  : Same as stdout.
				// fd 3 (channel) : Producer Channel fd.
				// fd 4 (channel) : Consumer Channel fd.
				stdio       : [ 'ignore', 'pipe', 'pipe', 'pipe', 'pipe' ],
				windowsHide : true
			});

		this.#pid = this.#child.pid!;

		this.#channel = new Channel(
			{
				producerSocket : this.#child.stdio[3],
				consumerSocket : this.#child.stdio[4],
				pid            : this.#pid
			});

So the problem is that this.#child.stdio[3] and this.#child.stdio[4] are undefined in Bun, meaning that they do not properly support the Node spawn() API, which BTW I think it’s an already known Bun limitation.

I will open issue in Bun Github and will update this thread as soon as things move.
Hope Mediasoup will run smoothly with bun :slight_smile:

Thank you Iñaki for your kind support.

It’s not an issue, this is a documented incompatibility: Node.js compatibility – Runtime | Bun Docs

Hi all,
For information, I tried with latest version of bun and this issue no more occurs.
I think bun’s documentation is not up to date or functionality is not yet fully supported.
Mediasoup demo still not work on bun probably because of incompatibility between bun and websocket library used internaly by protoo.
Regards