Next.js v14.0.0 server with socket.io client doesn't send messages with more than a few characters
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
With debugging -
Pass (abc)
Fail (abcd)
The only difference is engine.io-client:websocket displaying websocket closed before onclose event at then end.
Heartbeat is still working afterwards.
Here is a github repo to reproduce the error https://github.com/mecabricks/debug-socketio
To Reproduce
- yarn dev-ws
- yarn dev
- Make a
GETrequest to the pagehttp://localhost:3000/api/test - 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
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?
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;
},
For future readers:
Please check our guide with Next.js: https://socket.io/how-to/use-with-nextjs
Please reopen if needed.
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")