C
C
connecter2019-10-14 13:35:49
Node.js
connecter, 2019-10-14 13:35:49

How to connect socket.io to express when deploying to ZEIT?

Hello everyone, the question is, how to connect socket.io to express when building and deploying to ZEIT through now? On local in development mode, everything works.
I use Next.js + Express and upload it all to ZEIT.
now.js

"builds": [
      { "src": "server/**/*.js", "use": "@now/node" },
      { "src": "package.json", "use": "@now/next" }
    ],
    "routes": [
      { "src": "/api/(.*)", "dest": "/server/src/server.js" },
      { "src": "/socket.io/(.*)", "dest": "/server/src/server.js" },
      { "src": "/(.*)", "dest": "/$1" }
    ]

server.js (socket connection)
const _server = server.listen(8080, (err) => {
        if (err) throw err
        console.log('Server is started :)')
    })
    
    const io = socketIO(_server, { serveClient: false });

client
import io from 'socket.io-client';
const socket = io('https://url....', { secure: true });

After refreshing the page, sometimes everything works for a couple of seconds, but then in the console there are errors
5da44f7ec8796153432840.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RaShe, 2019-10-24
@RaShe

No way.
ZEIT is serverless, it will not connect to sockets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question