Pusher functionality is activated only upon page refresh

I've been working on integrating Pusher into a web application built with Next.js. While it functions correctly in my local development environment, I encounter issues when deploying it to Vercel. The data is only visible after refreshing the page in the browser.

Here's the client-side implementation:

useEffect(() => {

    const pusher = new Pusher(`${process.env.PUSHER_KEY}`, {
        cluster: `${process.env.PUSHER_CLUSTER}`,
        useTLS: true
    });

    const channel = pusher.subscribe('franks-auto-spa');
    channel.bind('cancel-wash', data => {
        console.log(data.date);
        removeWash(data.date);
    });

}, []);

And here's the API implementation:

export default async (req, res) => {

    const connection = await mysql.createConnection(process.env.DATABASE_URL);
    console.log(req.body.date);
    const result = await connection.query('DELETE FROM ongoing WHERE date = ?', [req.body.date]);
    console.log(result);

    const pusher = new Pusher({
        appId: `${process.env.PUSHER_ID}`,
        key: `${process.env.PUSHER_KEY}`,
        secret: `${process.env.PUSHER_SECRET}`,
        cluster: `${process.env.PUSHER_CLUSTER}`,
        useTLS: true
    });

    pusher.trigger('franks-auto-spa', 'cancel-wash', req.body);
    res.json({message: 'Wash deleted deleted...'});
}

Could there be any missing configurations in Vercel?

Answer №1

I've encountered this issue a few times as well; Vercel tends to time out after some time. (Hobby plan has a timeout of 10 seconds) Therefore, using a database with Vercel may not be feasible. Have you considered upgrading to the Pro plan? Alternatively, switching to Heroku could be an option since it offers a longer timeout (30 seconds) and also supports websockets.

Answer №2

Apologies for the delayed response, I just discovered this as well. Ensure to add 'await' to avoid disabling serverless features!

pusher.trigger('franks-auto-spa', 'cancel-wash', req.body);

change it to:

await pusher.trigger('franks-auto-spa', 'cancel-wash', req.body);

Answer №3

For those encountering a similar issue, patiently waiting for the execution of the pusher.trigger() method is the solution.

await pusher.trigger("channel-id", "incoming-message", "your-message")

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

What is the method to obtain the keycode for a key combination in JavaScript?

$(document).on('keydown', function(event) { performAction(event); event.preventDefault(); }); By using the code above, I am successful in capturing the keycode for a single key press. However, when attempting to use a combin ...

tips for exporting database information to an excel file with the help of ajax request and javascript

Recently, I built an application using NDWS with sapui5 _javascript. Within the application, there is a table that contains data synced with the server's database. My goal is to retrieve this data from the table and export it to an Excel document. Her ...

Having trouble with changing text in a link with an onclick event?

When I click on the link, I want the text to change to the second span. However, this functionality is not working. Code var reload = false; $('#change').click(function() { reload = !reload; $('#change').click(function() { ...

Encountering a "MissingSchemaError" while attempting to populate the database with mongoose-seeder

I am facing an issue while trying to populate a database using mongoose-seeder. Despite setting up the schema correctly, I keep encountering a MissingSchemaError which has left me puzzled. Here is a snippet from the file where I define the schema: const m ...

Why isn't my Next.js middleware working properly with TypeScript?

My issue arises from the fact that, despite following the documentation, the middleware in Next.js is not functioning as I anticipated. I experimented with what I thought was the simplest middleware possible. I expected that when navigating to /, a conso ...

The function document.querySelector functions correctly in the console but is not responsive in real-time

I'm currently working on an html page that includes a single audio player within an iframe. I am trying to implement autoplay functionality for both desktop and mobile devices. The specific player being used is displayed below: <div style="wi ...

How to extract a substring from a string in Javascript

I am currently facing an issue with extracting a specific part of a string (from the URL pathname) and I need some help to solve it. Here is what I have tried so far: ^(/svc_2/pub/(.*?).php) The string in question is: /svc_2/pub/stats/dashboard.php?aja ...

Understanding the extent of testing coverage in a project using karma and webpack

For my project, I am incorporating ES6 syntax and testing my code with Karma. While I have successfully set up testing, I encountered an issue with the coverage report. Instead of including the source code, the coverage report is highlighting spec file ...

What is the best way to ensure that each service call to my controller is completed before proceeding to the next one within a loop in Angular?

Calling an Angular service can be done like this: this.webService.add(id) .subscribe(result => { // perform required actions }, error => { // handle errors }); // Service Definition add(id: number): Observable < any > { retu ...

How can you use three.js to easily create a cylinder with a slice removed from it?

Can someone guide me on creating a 3D model of a cylinder with a slice cut out using three.js? Something similar to this: Image Your input is highly valued. Thank you. ...

Controlling the HTML5 dialog element within a React application

Struggling to implement a basic configuration dialog with a close icon in the top-right corner using React. In other frameworks, it's easy to show/hide a dialog with .showModal()/close(), but manipulating the DOM directly in React is not recommended. ...

Exploring a personalized approach to searching in DataTables

I am facing a dilemma with implementing a custom search in a DataTables table due to my limited experience with JavaScript. The function I have only triggers on startup and not when typing in the search box. If you're interested, you can find my orig ...

Stop removing event triggers when the close button on toastr is clicked

Incorporating toastr.js into my application has presented a unique challenge. When a user submits a form, I want to display a toast notification and provide them with a brief window of time to close the toast by clicking a button before sending the data to ...

What is the process for connecting a Wii Remote with Three.js?

Suppose I wanted to explore Wii Remote to browser interaction by connecting it to my Ubuntu laptop via Wiican and Wmgui using Bluetooth. What would be a simple program to achieve this? I have successfully used an Xbox Gamepad with Chrome, so I know that i ...

How can I retrieve the text input from a physical barcode scanner in a React Native screen?

Currently, I am developing a mobile application with React Native version 0.68.2. I am seeking a solution to detect barcode scanning events from a physical scanner hardware that is integrated into the mobile device as a handheld scanner. The key requirem ...

Show the ajax response on a separate page

I am looking to showcase the output of an ajax request on a separate page rather than the page where the ajax call originated. The scenario is that I have a membership directory page, and when a user clicks on a member ID cell, an ajax call sends the ID to ...

Highlight main title in jQuery table of contents

I have successfully created an automatic Table of Contents, but now I need to make the top heading appear in bold font. jQuery(document).ready(function(){ var ToC = "<nav role='navigation' class='table-of-contents vNav'>" + ...

What is the best way to send checkbox values to ActionResult in MVC5?

I am working on an MVC5 application and within my view, I have the following code snippet: <div class="form-group"> @Html.LabelFor(model => model.CategoryID, "Category", htmlAttributes: new { @class = "control-label col-md-3" }) <div c ...

Babel is not recognizing reactRouter2.default as it is undefined

I followed a tutorial to create a universal React app, but I encountered an error in Chrome Dev tools that says: 'Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a React ...

Sort an array by mapping it in decreasing order based on the total sum of its elements

I came across a JSON structure that looks like the following: { "user": [ {"username": "x1", "pfp": "", "scores": [{"easy": 10, "normal": 1, "hard": 2, "oni&q ...