Upgrading to version 12 of discord.js, I have noticed a huge jump in latency when performing Discord API calls. I am using virtually the exact same code to test this, other than the different variants of retrieving the bot's ping, and can see that the latency using the code below is very different between discord.js 11.4.2 and 12.0.0.
In the discord.js Discord guild, several other people have reported this same issue. I am unsure what is the root cause of it, perhaps something to do with internal sharding?
Discord.js v11.4.2:
Latency: 262ms
API Latency: 206ms
Discord.js v12:
Latency: 11736ms
API Latency: 222ms
Include a reproducible code sample here, if possible:
// Discord.js v11.4.2
const m = await message.channel.send("Ping test");
console.log(`Latency: ${m.createdTimestamp - message.createdTimestamp}ms`);
console.log(`API Latency ${Math.round(client.ping)}ms`);
// Discord.js v12
const m = await message.channel.send("Ping test");
console.log(`Latency: ${m.createdTimestamp - message.createdTimestamp}ms`);
console.log(`API Latency ${Math.round(client.ws.ping)}ms`);
Further details:
I can confirm a long latency or complete unresponsiveness when trying anything in the v12 API.
I checked that my code works in 11.4-dev, and it works flawlessly without latency.
I mean, I didn't experience this when I swapped from stable to master though I have a different method of calculating latency
<Message>.channel.send(`Pong! Ping is ${Date.now() - msg.createdTimestamp}ms.`);
I can reproduce this issue.
Received message events by the user appear pretty much immediately and so should do the message response by the bot, too, but it doesn't.
My API latency is about 100 ms fast, but the bot often has a ping of about 5000-15000 ms, which is way too high.
Would love to see this issue fixed as this problem made many users remove the bot from their server because of its slow response time.
I mean, I didn't experience this when I swapped from stable to master though I have a different method of calculating latency
<Message>.channel.send(`Pong! Ping is ${Date.now() - msg.createdTimestamp}ms.`);That method is not a viable option as you are comparing a local timestamp with a server timestamp, times from 2 different machines.
That method is not a viable option as you are comparing a local timestamp with a server timestamp, times from 2 different machines.
Really don't know how that works, I've been using this method for 5 months+ and never experienced any issues, the ping is consistent all the time and seems accurate, at moments where I feel it should be higher, it certainly is.
EDIT: It's entirely viable: https://github.com/discordjs/discord.js/blob/master/src/structures/Message.js#L110T
Date constructor:
Creates a JavaScript Date instance that represents a single moment in time. Date objects use a Unix Time Stamp, an integer value that is the number of milliseconds since 1 January 1970 UTC.
Meaning its the same timezone
EDIT: It's entirely viable: https://github.com/discordjs/discord.js/blob/master/src/structures/Message.js#L110T
Date constructor:
You're not understanding how this works.
The servers from Discord, and the server that hosts your bot, have different CPU clocks (which dictate the "millisecond" they're at when generating snowflakes, getting the current timestamp...). By this logic, it may be 19:50:30.100 for your PC, but 19:50:30.150 for Discord's servers, if not already off by several seconds (like in many cases).
The correct way to measure the timestamp is to send a message and edit it with the time difference between the command message's createdTimestamp with the reply message's createdTimestamp.
Meaning its the same timezone
UNIX timestamps do not have timezones, and they are the number that dictates the "date" for JavaScript's Date, and for many other languages in their respective equivalents, since this is an absolute unit.
Is this still an issue?
I have been trying on and off to reproduce this with current master (#3dff5058f0beae01cec0abe27e3a43c8fd7c60ce) and have been getting constantly 80-300ms ping (which is expected for the system I use).
The bot I use is not sharded or anything fancy, just a one guild bot, and I can't seem to get the same issue.
Got the same latency problem
Might be unrelated but I found similarly unusual slowness when resolving Promise.all, but it was related to the uws package I had installed, it was behaving differently between stable/11.4-dev and master.
If you have uws installed, try removing it and trying again?
for selfhost its normal, but on cloud host its really bad
Might be unrelated but I found similarly unusual slowness when resolving Promise.all, but it was related to the
uwspackage I had installed, it was behaving differently between stable/11.4-dev and master.If you have
uwsinstalled, try removing it and trying again?
Yeah, the maximum latency I can get without uws is 350ms (Min 211ms), meanwhile, the minimum latency I can get with uws is 2303ms (Max 5977ms), they both have an API latency of ~235ms, it seems like the problem is from @discordjs/uws.
For now, the temporary solution for me at least is to not use @discordjs/uws.
Yeah that’s exactly what I did, stopped using it pretty much immediately. The package is deprecated now anyway, but it’s good to know I’m not the only one who encountered that.
I can confirm that removing @discord/uws resolved my high latency issue. It seems the problem lies there.
I'd advise against using uWs from this point on. It's not worth maintaining an old version of it anymore.
So for anyone that reads this, just don't use uWs anymore, especially not on newer node versions.
Most helpful comment
Yeah, the maximum latency I can get without
uwsis 350ms (Min 211ms), meanwhile, the minimum latency I can get withuwsis 2303ms (Max 5977ms), they both have an API latency of ~235ms, it seems like the problem is from@discordjs/uws.For now, the temporary solution for me at least is to not use
@discordjs/uws.