socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

Next.js v14.0.0 server with socket.io client doesn't send messages with more than a few characters

Open njarraud opened this issue 2 years ago • 4 comments

Describe the bug Socket.io-client installed on a Next.js v.14.0.0 server doesn't send messages if they are longer than a few characters.

The issue appeared between Nextjs v.13.5.7-canary11 and v13.5.7-canary12. Everything is working as expected for previous versions.

To Reproduce Socket.IO server version: 4.7.2 Socket.IO client version: 4.7.2 Nextjs version: 14.0.0 on nodejs runtime

Server

import { Server } from 'socket.io';

const nextNamespace: NextSocketNamespace = io.of('/next');

nextNamespace.on('connection', (socket) => {
  socket.onAny((eventName, ...args) => {
    console.log(`Received event "${eventName}" with data: ${JSON.stringify(args)}`);
  });
});

Client

Code shall be triggered in a API route for example - not in the browser

import { io } from 'socket.io-client';

const websocket: NextSocketClient = io('ws://localhost:3000/next`);

websocket.emit('partLibraryIconEvent', { test: 'abcd' });

Expected behavior The message Received event "partLibraryIconEvent" with data: [{"test":"abcd"}] shall be logged on the server console. However nothing is logged.

It is working if a character is removed anywhere -

  • [x] websocket.emit('partLibraryIconEvent', { test: 'abc' });
  • [x] websocket.emit('partLibraryIconEvent', { t: 'abcdef' });
  • [x] websocket.emit('partLibraryIcon', { test: 'abcdefgh' });

I guess it is serialized as ["partLibraryIconEvent",{"test":"abc"}] If this is the case, it seems that 39 characters is the maximum. Anything above and the message is not sent.

Platform: Any

njarraud avatar Nov 02 '23 22:11 njarraud

With debugging -

Pass (abc) Capture d’écran 2023-11-03 à 14 26 47

Fail (abcd) Capture d’écran 2023-11-03 à 14 25 54

The only difference is engine.io-client:websocket displaying websocket closed before onclose event at then end.

Heartbeat is still working afterwards.

njarraud avatar Nov 03 '23 01:11 njarraud

Here is a github repo to reproduce the error https://github.com/mecabricks/debug-socketio

To Reproduce

  1. yarn dev-ws
  2. yarn dev
  3. Make a GET request to the page http://localhost:3000/api/test
  4. See the logs in the socket.io server terminal window

Current vs. Expected behavior The socket.io server shall receive the 3 messages sent by the socket.io client (server side) however only two are received - The two with the same length. The longest is not sent over the network.

socket.emit("partLibraryIconEvent", { test: "abc" });   // OK
socket.emit("partLibraryIconEvent", { test: "abcd" });  // NOT OK
socket.emit("partLibraryIconEvent", { test: "123" });   // OK

njarraud avatar Nov 03 '23 20:11 njarraud

Experiencing the same, had to move all server code using websockets back to pages dir api routes.

I'm wondering why the websocket gets closed. Do we need to set some extra response headers on routes using websockets? Is this even a socket.io issue or a next.js issue?

filipjnc avatar Nov 08 '23 16:11 filipjnc

I believe that NextJS is overwriting something when packing the code. We cannot figure out what. However, the solution that I just found is to exclude the socket.io-client package from webpack on the server.

Here is the code to go in next.config.js

webpack: (config, { isServer }) => {
  isServer && (config.externals = [...config.externals,  'socket.io-client']);
  return config;
},

njarraud avatar Nov 09 '23 03:11 njarraud

For future readers:

Please check our guide with Next.js: https://socket.io/how-to/use-with-nextjs

Please reopen if needed.

darrachequesne avatar Apr 08 '24 15:04 darrachequesne

I have fixed this issue by identifying the Nextjs server in the same event that I emit. example socketio.emit("YOUR_CLIENT_IDENTIFIER","ROOM","DATA")

mohammedothmanintegrant avatar Aug 06 '24 14:08 mohammedothmanintegrant