Guide on deploying a web application using Socket.io and SvelteKit on Vercel

Currently, I'm developing a multiplayer .io-style game that utilizes both Socket.io and SvelteKit. While testing the project on a local development server, everything runs smoothly. However, upon deploying to Vercel, an error message stating

Failed to load resource: the server responded with a status of 404 ()
is logged.

Below is my current express server.ts code snippet (not functional)

import http from "HTTP";
import { handler } from './build/handler.js';
// Encounters an error: An import path cannot end with a '.ts' extension.
import injectSocketIO from "src/lib/socket-handler.ts";
import express from 'express';

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

injectSocketIO(server)

app.use(handler);

// Is there something that needs to be modified here?
server.listen(3000, () => {
    console.log('Running on http://localhost:3000');
});

To run a local development server using the vite plugin, I input the following:

import injectSocketIO from "./src/lib/socket-handler";

plugins: [sveltekit(), {
    name: "sveltekit-socket-io",
    configureServer(server) {
        injectSocketIO(server.httpServer);
    },
}],

I have attempted to follow the guidelines provided here. However, I am using socket-handler.ts instead of socket-handler.js and am unsure about how to establish a startup script for execution with Vercel. Any assistance on this matter would be greatly appreciated!

Answer №1

After much searching, I found a solution that worked for me. By moving the Express server outside of the Svelte src directory, I was able to resolve the same issue.

My approach mirrored yours, with the only difference being that I imported socket-handler.js from the root directory:

// server.js
import express from 'express'
import injectSocketIO from './socket-handler.js'
import { handler } from './build/handler.js'

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

// Integrate SocketIO
injectSocketIO(server)

// Handle SvelteKit requests
app.use(handler)

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000')
})

To implement this change, simply run

npm run build && node server.js
to compile the Svelte app and launch the Express server.

This guide pointed me in the right direction. Essentially, by setting up a traditional Express server, integrating socket.io, and leveraging SvelteKit's Express middleware capabilities, the issue was successfully addressed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to make an angular component reuse itself within its own code?

I am dealing with a parent and child component scenario where I need to pass data from the parent component to the child component. The aim is to display parentData.name in the child component when parentData.isFolder===false, and if parentData.isFolder=== ...

Retrieve active route information from another component

We are utilizing a component (ka-cockpit-panel) that is not linked to any route and manually inserted into another component like so: .. ... <section class="ka-cockpit-panel cockpit-1 pull-left"> <ka-cockpit-panel></ka- ...

Experiencing difficulties when trying to set up the express/body-parser node js eclipse

I'm currently working my way through the instructions for setting up express web servers using the eclipse/node js plugin in a tutorial book, and I am struggling to install express or create the necessary subfolders as instructed. Despite following th ...

How about this: "Harness the power of nodeJS, express, and jqtpl

Having an issue with NodeJS as I am still new to it. Currently, I am using nodeJS, express, and JQTPL to follow a tutorial on calling the Github API. However, the problem lies in the Application configuration, specifically in the engine definition. va ...

ERROR: Unexpected issue occurred with v8::Object::SetInternalField() resulting in an internal field going out of bounds while utilizing node-cache in Node.js

I recently started working with API exports that contain a large amount of data, so I decided to utilize the node-cache in order to speed up the API response time, as it was taking more than 2 minutes to retrieve the data. Being new to this, I came across ...

Snackbar and RTK Query update trigger the error message: "Warning: Cannot update during an existing state transition."

I've built a basic ToDos application that communicates with a NodeJS backend using RTK Query to fetch data, update state, and store cache. Everything is functioning properly as expected with the communication between the frontend and backend. Recently ...

Implementing an Asynchronous Limited Queue in JavaScript/TypeScript with async/await

Trying to grasp the concept of async/await, I am faced with the following code snippet: class AsyncQueue<T> { queue = Array<T>() maxSize = 1 async enqueue(x: T) { if (this.queue.length > this.maxSize) { // B ...

Navigating through nested Firebase realtime DB queries using await/async techniques

In the process of developing a Firebase function (Gist), I encountered a challenge that I'm seeking assistance for. The function starts by querying a realtime database reference (events) using this code: await admin.database().ref('/events_geo ...

Angular 2 - Can a Content Management System Automate Single Page Application Routing?

I am interested in creating a single-page application with an integrated content management system that allows users to edit all aspects of the site and add new pages. However, I have found it challenging to configure the SPA to automatically route to a n ...

Error in AngularX TS: Trying to invoke a type that does not have a callable signature

Encountering an issue while working on a component, specifically during ng serve/build process. Please note that this error is different from any console errors, despite what some may think. The expected outcome is for the code to successfully build and ru ...

How can I combine HTML and CSS in a single request using the sendFile method in Express?

const app = require('express')(); app.get('/', function(req, res) { res.sendFile(__dirname + "/" + "index.html"); }); <link rel="stylesheet" href="style.css"> I utilized the Node.js code above to send an HTML file. In order to ...

Is it necessary to incorporate express in a Next.js project?

I'm currently working on a website using Next.js. With Next.js, I have access to features like SSR and dynamic routing. Is it necessary for me to incorporate express into my project? If yes, what are the reasons behind needing to use it? What unique ...

Is it possible to integrate the Firestore npm library into my Express application?

Recently, I created my own library to act as a nosql database on my node.js web server in place of mongodb. I came across this interesting quote: Applications that use Google's Server SDKs should not be used in end-user environments, such as on pho ...

Is there a more effective method to return a response apart from using a redundant function?

function unnecessaryFunction(){ let details: SignInDetails = { user: user, account: account, company: company }; return details; } I am being told that the details value is unnecessary. Is there ...

Saving a group of selected checkboxes as an array in Ionic: a step-by-step guide

I am working on a simple form with checkboxes and I need to send only the checked boxes to TypeScript. However, when I attempt to save the data by clicking the save button, I encounter an error message {test: undefined} Below is the form layout: <ion-c ...

Receiving an error message while attempting to mount an API on a different route in ExpressJS: "Encountered an issue reading the property 'handle'

I am currently working on a project where I want to load my static files on the root / URL, and have my API mounted at /api. This is the main file I am using: var express = require('express'); var server = express(); var app = require('./a ...

Error: Unable to simplify version range

Attempting to follow the Express beginner's guide. I created an app and executed npm install as per the provided instructions. > npx express-generator --view=pug myapp > npm install npm ERR! semver.simplifyRange is not a function npm ERR! A com ...

What is the process for implementing a new requirement for 'confirmed' users when they log in with passport.js?

Looking to enhance the functionality of passport.js by implementing a conditional check to verify if the user account is confirmed. The plan is to insert an 'if statement' towards the end of the code, right after validating the user's email ...

Tips for looping through client.get from the Twitter API with node.js and express

I am in the process of developing an application that can download a specific number of tweets. For this project, I am utilizing node.js and express() within my server.js file. To retrieve data from the Twitter API, I have set up a route app.get('/ap ...

Is there a way to utilize a value from one column within a Datatables constructor for another column's operation?

In my Typescript constructor, I am working on constructing a datatable with properties like 'orderable', 'data' and 'name'. One thing I'm trying to figure out is how to control the visibility of one column based on the va ...