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

usage with Socket.io #138

Open
1 task
pi0 opened this issue Feb 13, 2025 · 1 comment
Open
1 task

usage with Socket.io #138

pi0 opened this issue Feb 13, 2025 · 1 comment
Labels
discussion documentation Improvements or additions to documentation

Comments

@pi0
Copy link
Member

pi0 commented Feb 13, 2025

Describe the change

Discussion from sveltejs/kit#1491 (comment)

socket.io has an example(ish) about integrating with crossws (which only works within node.js + depending on _internal)

Made this issue also to think how better we can deal with this kind of integration...

We could have an integrations page in docs to list other integrations like y-crossws (there could be also socketio-crossws community package to at least have better internal conditional checks)

URLs

No response

Additional information

  • Would you be willing to help?
@pi0 pi0 added documentation Improvements or additions to documentation discussion labels Feb 13, 2025
@nerg4l
Copy link

nerg4l commented Feb 14, 2025

I will share my attempt. Again, sorry if this is off-topic. With the code below, I could trigger the connect (Socket.IO connected) but not the message (Socket.IO message) event. If I add message crossws hook, I can see that the server receives the event but not Socket.IO.

At the moment I just want to better understand how engine.io works and how it can be integrated with crossws.

import { createServer } from "node:http";
import crossws from "crossws/adapters/node";
import { Server as Engine } from "engine.io";
import { Server } from "socket.io";
import * as url from "node:url";

const engine = new Engine();
const io = new Server();

io.bind(engine);

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

  socket.on("message", (data) => {
    console.log(`Socket.IO message: ${data}`);
  })

  socket.on("error", (err) => {
    console.log(`Socket.IO error: ${err}`);
  })

  socket.on("disconnect", () => {
    console.log(`Socket.IO disconnected`);
  })
});

const ws = crossws({
  hooks: {
    open(peer) {
      // @ts-expect-error private method and property
      engine.prepare(peer._internal.nodeReq);
      // @ts-expect-error private method and property
      engine.onWebSocket(peer._internal.nodeReq, peer._internal.nodeReq.socket, peer.websocket);
    }
  },
});

const server = createServer().listen(3000);

server.on("request", (req, res) => {
  const u = url.parse(req.url, true);
  if (u.pathname === "/socket.io/") {
    engine.handleRequest(req, res);
    return
  }
  if (u.pathname === "/") {
    res.end(
      `<script src="https://cdn.socket.io/4.8.1/socket.io.min.js" integrity="sha384-mkQ3/7FUtcGyoppY6bz/PORYoGqOl7/aSUMn2ymDOJcapfS6PHqxhRTMh1RR0Q6+" crossorigin="anonymous"></script>
<script>
    const socket = io('http://127.0.0.1:3000');
    socket.on('connect', () => {
      socket.emit('hello from client');
    });
</script>`,
    );
    return;
  }
  res.statusCode = 404;
  res.end(``)
})

server.on("upgrade", (req, socket, head) => {
  if (req.headers.upgrade === "websocket") {
    ws.handleUpgrade(req, socket, head);
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants