nodejs / undici

An HTTP/1.1 client, written from scratch for Node.js
https://nodejs.github.io/undici
MIT License
5.91k stars 511 forks source link

WebSockets #1811

Open KhafraDev opened 1 year ago

KhafraDev commented 1 year ago

permessage-deflate support


Performance


Tests


Bugs


Features


WebSocketStream

jimmywarting commented 1 year ago

Note: we need support in node core to read a Blob synchronously.

Hmm, would that be really be any good? if we ever get something like blob backed up by the fs and you would be able to create them from a network mounted cloud drive. then that would block quite a bit.

KhafraDev commented 1 year ago

would that be really be any good?

No, but it's the only way that issue could be fixed without adding complexity.

jimmywarting commented 1 year ago

where thinking about trying out websocket today but couldn't find any documentation of how to use it. eg how to upgrade a request connection

KhafraDev commented 1 year ago

Undici only implements a websocket client, the spec one that has documentation on mdn. If you want to use the lower level dispatcher api from undici, you can use the onUpgrade callback.

jimmywarting commented 1 year ago

Hmm, thanks for that helpful documentation (kind of what i was looking for), will try it out, it would be pretty sweet if there where a upgrade method to turn a connection into a standard WebSocket


ws made this very easy to use

import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function message(data) {
    console.log('received: %s', data);
  });

  ws.send('something');
});
mcollina commented 1 year ago

Implementing a server is definitely out of scope: use ws - none of the spec-compliant work has the settings or APIs needed to implement a robust WebSocket server. On another note, we should likely document this somewhere, as this question would come up quite often.

jimmywarting commented 1 year ago

none of the spec-compliant work has the settings or APIs needed to implement a robust WebSocket server

What would it take to maybe say: Build a own npm package that depends on undici WebSocket and tries to build a compatible server? would it be even at all possible by using import xyz from 'undici/lib/websocket/*'? and would it require a lot of work?

I'm guessing it would have to work quite differently if you used http/1/2/3 quick WebTransport or whatever is the new "thing"

we should likely document this somewhere, as this question would come up quite often.

I bet so too. Guess ppl are going to ask about this when they learn that there is a built in WebSocket later

yume-chan commented 1 year ago

Is there any update on the "Setting an undici Dispatcher rather than using the global dispatcher by default." feature?

I need to send each WebSocket to different targets without modifying the Host header (the server checks it). By creating multiple dispatchers I can use the Dispatcher#connect method to override the target. But now I have to use this hack:

const agent = new Agent({
    factory(origin, opts) {
        const pool = new Pool(origin, {
            ...opts,
            factory(origin, opts) {
                const client = new Client(origin, opts);
                // Remote debugging validates `Host` header to defend against DNS rebinding attacks.
                // But we can only pass socket name using hostname, so we need to override it.
                (client as any)[Symbols.kHostHeader] = "Host: localhost\r\n";
                return client;
            },
        });
        return pool;
    },
    async connect(options, callback) {
        try {
            const socket = await device.createSocket(
                "localabstract:" + options.hostname
            );
            callback(null, new AdbUndiciSocket(socket) as unknown as Socket);
        } catch (e) {
            callback(e as Error, null);
        }
    },
});
// WebSocket only uses global dispatcher
setGlobalDispatcher(agent);

I'm also concerned that undici used in other libraries will also be affected by my global dispatcher.

KhafraDev commented 1 year ago

Is there any updates on the "Setting an undici Dispatcher rather than using the global dispatcher by default." feature?

Yes, I'm planning on implementing my proposal which will allow us to do so. I should have a PR in the next day, if I remember.