Home > database >  Deploy sveltekit app on vercel using node adapter
Deploy sveltekit app on vercel using node adapter

Time:02-01

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

CodePudding user response:

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

  • Related