socketio / socket.io-client

Realtime application framework (client)
https://socket.io
10.62k stars 3.04k forks source link

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

Closed njarraud closed 5 months ago

njarraud commented 11 months ago

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 -

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 commented 11 months ago

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 commented 11 months ago

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
filipjnc commented 11 months ago

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?

njarraud commented 11 months ago

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;
},
darrachequesne commented 5 months ago

For future readers:

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

Please reopen if needed.

mohammedothmanintegrant commented 1 month ago

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")