Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for WebSockets #7

Open
EricEisaman opened this issue Jan 18, 2020 · 3 comments
Open

Support for WebSockets #7

EricEisaman opened this issue Jan 18, 2020 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@EricEisaman
Copy link

Do you have an idiomatic example for establishing a WebSocket with pogo?

@sholladay
Copy link
Owner

Hi and thanks for checking out Pogo!

I have not yet tried to use WebSockets with Deno. There is no API in Pogo for it currently, but I am open to adding support for WebSockets. That will happen faster if you can make a pull request, otherwise I will probably get around to it eventually.

Here are some relevant resources:

  • Deno's WebSocket API in std, which we will use to support the protocol: https://github.com/denoland/deno/tree/master/std/ws
  • For inspiration about what the Pogo API should look like, see @hapi/nes (the API doesn't have to be the same if we come up with something better and it also doesn't have to be a plugin)

@sholladay sholladay changed the title WebSocket Support for WebSockets Jan 18, 2020
@sholladay sholladay added enhancement New feature or request help wanted Extra attention is needed labels Jan 18, 2020
@EricEisaman
Copy link
Author

Thanks. I will take a look.

@headdu
Copy link

headdu commented Apr 11, 2020

Hey!

I'm still learning Deno but I have something monkey patched working locally for me.

I'm not confident enough yet to write a PR but I'll leave here what I have for others that may want to test this out.

import this from deno's std

import {
  acceptWebSocket,
  acceptable,
} from "https://deno.land/std/ws/mod.ts";

My function to create the websockets is this one (Connection in this context is a class I created to manage all messages)

async function handleWebsocket(req: ServerRequest) {
  const { headers, conn } = req;

  try {
    const websocket = await acceptWebSocket({
      conn,
      headers,
      bufReader: req.r,
      bufWriter: req.w,
    });

    new Connection(websocket);
  } catch (err) {
    console.error(`failed to accept websocket: ${err}`);
  }
}

Initialize Pogo as per the Readme

const server = pogo.server({ port: 8080 });

server.router.get("/", (req: any) => {
  console.log(`request received`);

  return "Hello!";
});

And now for the important bit. Instead of starting pogo normally, I created a different function just to check if it is a valid websocket request, if it is manage it with the previously created function

async function start() {
  const serverr = serve({
    hostname: "localhost",
    port: 8080,
  });
  server.raw = serverr;
  for await (const request of serverr) {
    if (acceptable(request)) {
      await handleWebsocket(request);
    } else {
      server.respond(request);
    }
  }
}

await start();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants