1

I'm trying to deploy my app on vercel using node adapter. I know that i should use the vercel adapter but i made some changes to make sveltekit work with socket.io. I followed this guide: https://joyofcode.xyz/using-websockets-with-sveltekit.

The Node adapter creates the index.js and handler.js files in your build folder when you run npm run build — creating a custom server works by importing the handler from build/handler.js and using your custom server instead of index.js. Then I created a index.js file at the root of the project with the following content:

import http from 'http';
import express from 'express';
import injectSocketIO from './src/lib/websockets/socketIoHandler.js';
import { handler } from './build/handler.js'

const app = express();
const server = http.createServer(app);

// // Inject SocketIO
injectSocketIO(server);

// // SvelteKit handlers
app.use(handler);

server.listen(3000, () =\> {
console.log('Running on http://localhost:3000');
});

i tryied in some ways to make the deploy but I can't make it work...

How could I deploy the app on vercel? Thanks!

enter image description here using this vercel.json file:

{
  "version": 2,
  "builds": [
    {
      "src": "./index.js",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/web/(.*)",
      "dest": "/"
    }
  ]
}

enter image description here

2 Answers 2

1

To put this simply, the adapter-vercel exists, because Vercel mainly works on the front-end aspect and is serverless. socket.io will not work on Vercel mainly because Vercel will freeze your app (including your socket) after it detects an inactivity in the application.

So TLDR; you can't use socket.io on Vercel. You can see more on that here: https://vercel.com/guides/do-vercel-serverless-functions-support-websocket-connections

Sign up to request clarification or add additional context in comments.

Comments

0

You could use the automatic adapter:

import adapter from '@sveltejs/adapter-auto';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    kit: {
        adapter: adapter()
    }
};

export default config;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.