socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Deno support

Open ariesclark opened this issue 5 years ago • 9 comments

  • [x] request a feature

will there be official support for https://deno.land?

ariesclark avatar May 21 '20 08:05 ariesclark

Any news about this feature?

lupodevelop avatar Oct 06 '20 14:10 lupodevelop

Does anybody know what is needed for supporting deno?

darrachequesne avatar Oct 06 '20 20:10 darrachequesne

Deno for client uses the web WebSocket API, whereas for server you could look at how it is done in https://deno.land/[email protected]/ws/mod.ts

crowlKats avatar Oct 07 '20 03:10 crowlKats

Any update, does socket.io works with deno?

al6x avatar Apr 12 '21 04:04 al6x

You ca try to use: http://esm.sh/socketio but I had a talk in the discord chan of deno and it seems that the node modules needs to be converted. I also wanted to use ws with deno but they don't have support for a socks proxy. So trying the package that I mentioned, wait for official support or helping by make the underlying network stuff of node compatible :).

Also if you haven't tried it yet: https://stackoverflow.com/questions/61821038/how-to-use-npm-module-in-deno/61821141#61821141

Chris2011 avatar Dec 07 '21 20:12 Chris2011

There's a pretty good tool called deno2node. It lets you write Deno modules in TS and then it can compile them to JS, just like tsc compiles TS to JS. Using it in a few dozen projects without problems. If there are things that work differently on both platforms (such as maybe sockets) you can provide two different source files for this part of the library. That way, you can basically provide the same module for both runtimes, and a tiny bit under the hood can vary.

Plan of attack would be:

  1. Port the code to Deno (sounds like more effort than it is, basically just adjust import statements)
  2. Along the way, extract Node-specific code into a Node-specific file
  3. Provide the Deno equivalent for this file
  4. Swap out tsc with deno2node
  5. Probably some config is broken, but otherwise you are done now.

Would be awesome to have Deno support!

KnorpelSenf avatar Jan 25 '22 18:01 KnorpelSenf

As a simpler alternative to the above mentioned solution, there is the official (experimental!) deno tool dnt, which allows you to create Node.js packages from deno ones.

If socket.io mantainers want to do this and want help to port socket.io to deno, feel free to contact me, i'd gladly help out.

crowlKats avatar Jan 25 '22 21:01 crowlKats

What i forgot to mention above with my comment: that isnt a perfect solution. Deno does provide a --compat flag which allows one to use node packages, or one can use https://esm.sh/. these are more robust solutions, and are more recommended over using dnt (in the end, they are completely different things, and have different goals, for the case of socket.io, i would actually rather recommend one of the former solutions, not dnt).

crowlKats avatar Jan 25 '22 21:01 crowlKats

I have tried dnt a lot and it forces you to completely change the project structure to become a Deno project. dnt is the right tool if you have a Deno project, and you want to create a new npm package from it. The other way around does not work well. I tried to provide feedback to the Deno team about this, but they deem this kind of compatibility out of scope. We are supposed to write custom build scripts which copy around all files for dnt to work, so I concluded that it is the wrong tool for when you already have an existing npm package and you want to have the least disruption.

In contrast, deno2node is literally meant to replace tsc, so it just fits in. Naturally, this comes at the cost that you now are responsible for the usual yada-yada with the Node config, so again, for fresh projects dnt is probably best.

But either way, I do agree that the compat mode would probably work, too, and it may be the easiest of all suggested solutions. Although it will of course not be as nice as having native Deno support.

KnorpelSenf avatar Jan 25 '22 22:01 KnorpelSenf

Hello everyone!

I'm happy to announce that we have published a Socket.IO server implementation for Deno :rocket:

Usage:

import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { Server } from "https://deno.land/x/[email protected]/mod.ts";

const io = new Server();

io.on("connection", (socket) => {
  console.log(`socket ${socket.id} connected`);

  socket.emit("hello", "world");

  socket.on("disconnect", (reason) => {
    console.log(`socket ${socket.id} disconnected due to ${reason}`);
  });
});

await serve(io.handler(), {
  port: 3000,
});

Link: https://deno.land/x/[email protected] Source: https://github.com/socketio/socket.io-deno Announcement: https://socket.io/blog/socket-io-deno/

Feedback is welcome!

darrachequesne avatar Sep 12 '22 00:09 darrachequesne

Welcome to the future, Socket.io devs. I just can't wait to stop finding Node examples online forever.

suchislife801 avatar Sep 13 '22 20:09 suchislife801