简体   繁体   English

推送器仅在刷新页面后工作

[英]Pusher working only after refreshing page

I'm implementing pusher in a web application using nextjs.我正在使用 nextjs 在 Web 应用程序中实现推送器。 It works as expected in my development environment.它在我的开发环境中按预期工作。 But it does not work correctly when I deploy it to vercel.但是当我将它部署到vercel时它不能正常工作。 I can only see the results when I refresh the page in the browser.只有在浏览器中刷新页面时才能看到结果。

This is the implementation for the client:这是客户端的实现:

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 this is the implementation for the API:这是 API 的实现:

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...'});
}

Am I missing any configuration in Vercel?我是否缺少 Vercel 中的任何配置?

I've had this problem too, once or twice;我也遇到过这个问题,一两次; Vercel times out after a little bit. Vercel 在一点点后超时。 (10 seconds on Hobby) So using a database with Vercel probably isn't gonna fly. (Hobby 10 秒)所以使用 Vercel 的数据库可能是行不通的。 Have you tried switching to the Pro plan?您是否尝试过切换到 Pro 计划? Or you could switch to Heroku , which not only has a longer timeout (30 seconds), but also supports websockets .或者你可以切换到Heroku ,它不仅有更长的超时时间(30 秒),而且还支持 websockets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM