Hacker News new | past | comments | ask | show | jobs | submit login
Deno Deploy Demo: a multi-datacenter chat, client+server in 23 lines of TS (deno.com)
186 points by ondras 79 days ago | hide | past | favorite | 45 comments

This is such a neat demo. I wrote up an annotated version of the code while I was figuring out exactly how it worked. https://til.simonwillison.net/deno/annotated-deno-deploy-dem...

Thanks for sharing!

I expect the demo works with or without the await on the listen promise because the listen code runs regardless (and prevents main from exiting).

Fantastic, thanks.

Deno is such a nice little scripting tool. I've been using it for devops glue and scripts lately.

Native typescript, easy-as-pie dependency management. its great!

Personally I do not like the fact that it puts too much trust into DNS.

DNS wasn't created with security in mind, so to say Deno is secure because it uses a permissions system while relying directly into DNS for imports to me sounds weird.

How is this any different than any other package management system? If you npm or pip install a package and import it, you’re still relying on DNS to resolve pypi or npmjs.

But with deno, if you’ve just accidentally installed some malicious squatters’ package that mimics the one you meant to use, at least you’ve got less exposure because the malicious code won’t necessarily have blanket permissions to make network calls, read files, etc. virtually any other language/package management system cannot say the same.

deno will download and cache the dependencies you import, they’re right in the project folder vs buried in a sea of node_modules. It’s super convenient to look at and inspect, if there’s any doubt or idle curiosity.

It's not just DNS, thankfully - deno.land has a TLS certificate too, which I think means that DNS exploits wouldn't succeed in causing clients to install untrusted packages.

It uses https. Are you not satisfied that that is reasonably secure?

I agree - also rock solid IDE support helps. It's really lightyears ahead of any other scripting options (Bash, Python, etc.)

Do you have any libraries you use often with scripts or mostly just deno built-ins?

What I really like about this is that you open the dependency in a web browser and see the human readable source code. No transpiling! https://deno.land/std/http/server.ts

This is generally already possible with webpack, but many/most websites i've looked at have source maps turned off. Maybe the extra compile time is too much, or they just want to disincentivize client-side reverse engineering.

It's the latter.

There's few things as fun as a multiplayer notepad that wasn't intended to be an actual chat, before it's invaded by bots and spammers. Congrats on the fella that tried to paste an ascii art version of Pepe the Frog.

Thanks for posting pepe, my friend

yes, it was me all along


update: the original version was too spammy, i have changed it so that pepe only responds to greetings or when you type "pepe"

update 2: i have throttled pepe so it only comes back after minute of cooldown

Nice, I like how short and simple it is. The title seems to imply it runs on multiple datacenters. How does it sync messages between those?

I was also wondering how this worked, and the following Deno Deploy documentation explains it: https://deno.com/deploy/docs/runtime-broadcast-channel/#broa...

I believe the BroadcastChannel implements the server-side distributed messaging.

Neat, it’s a server-side implementation of the browser API for tab-to-tab communication.


    class BroadcastChannel
      return `system('ircd')`
Only three lines! :D

Right, as jitl suggested. The BroadcastChannel is used to allow cross-datacenter messaging. So every script instance has its own set of clients and forwards incoming client messages to other script/datacenter instances.

I suppose this is not limited to multiple datacenters, but to multiple script instances in one datacenter as well.

Here's a version that resets the input field when you hit enter: https://dash.deno.com/playground/careful-toad-72

Has anyone written a comparison of Deno Deploy and Cloudflare Workers yet?

Deno: 28 datacenters Workers: 200+ datacenters

Deno: written by a guy who regrets his previous attempt at writing a programming environment

CloudFlare: global, huge company who regrets they don't already route 100% of internet traffic

Edit: I should add, I commend both efforts, both probably improve the world more than they hurt it, just injecting a bit of humor.

Not that simple of a comparison since "data centers" mean a different thing in both cases

I'm a bit confused when people say "native TypeScript" and "no transpiling" about Deno, does Deno now execute TypeScript without translating it to JavaScript first?

It always did. That’s been a core feature / benefit from day 1.

No, it never did, Deno always have converted TypeScript into JavaScript that then gets read and executed by the browser, at least according to their own docs but maybe that has changed lately?

> At a high level, Deno converts TypeScript (as well as TSX and JSX) into JavaScript. It does this via a combination of the TypeScript compiler, which we build into Deno, and a Rust library called swc. When the code has been type checked and transformed, it is stored in a cache, ready for the next run without the need to convert it from its source to JavaScript again.


My answer wasn’t explicit enough. To clarify, I meant as a user of deno you do not need to perform any separate transpilation or build step. The runtime can read and execute typescript natively (again, from user perspective).

>Deno can run JavaScript or TypeScript out of the box with no additional tools or config required.

- https://deno.land/manual@v1.15.3/examples/hello_world

>Deno is a runtime for JavaScript/TypeScript which tries to be web compatible and use modern features wherever possible.

- https://deno.land/manual@v1.15.3/getting_started/first_steps

You can run any of their examples to see that `deno run` commands can execute typescript without requiring a separate transpilation step

Yeah, I guess if someone asks "does Deno translate TS to JS before executing it?" and someone answers "Yes, it could always do this", it's not hard for the answer to be understood as "Deno doesn't translate TS before running it".

No hard feelings though, context is important and many of us miss it at times :)

It's native in so far as it embeds the transpiler and hides it from the user. It is still JavaScript under the hood.


so question - normally the thing I like about Node.js is not much context switching and ability to rerun code between backend and frontend. I can get the same thing with TS if I compile in both backend and frontend - if in Deno I don't compile and in the frontend I do are there any weird gotchas I would run into?

Deno is aiming to have better compatibility with web standards than Node, so you should encounter fewer gotchas from reusing code in backend and frontend.

love the chat spamming going on…

what is TS?


what, I'm just supposed to know that?

Nothing is to be known by everybody. But it's surprising that you don't associate typescript with deno. Unless you didn't know about deno in the first place.

Just saying because TS had some kind of popularity these days.

When discussing Deno, yes.

I think it's not unobvious if you open the link

To be fair, there is absolutely zero information about what technology is being used on the page the submission links to. Clicking the top-left icon brings you to "https://dash.deno.com/projects" which then forces you to login, still not giving any indication on what this thing is about or even written in.

I can see how you can get confused, even if I wasn't as I'm around the JS community.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact