mosca
mosca copied to clipboard
Mosca take so much CPU usage on server side. Is it a problem?
When I start my app with mosca, I always hear the Mac fan running loudly and hot. I see the memory watcher, node process will take 68% cpu for running. I just write a sample demo which will take so much CPU.
Is it something wrong on my code? The mosca version is 2.5.1.
const mosca = require('mosca')
const http = require('http')
const express = require('express')
const settings = {
backend: {
type: 'mongo',
url: `mongodb://127.0.0.1:27017/mosca`,
pubsubCollection: 'chatChannel',
mongo: {}
}
}
const server = express()
const httpServer = http.createServer()
const mqtt = new mosca.Server(settings)
mqtt.attachHttpServer(httpServer)
mqtt.on('clientConnected', function (client) {
console.log('client connected', client.id)
})
mqtt.on('published', function (packet, client) {
console.log('Published', packet.payload)
})
mqtt.on('ready', () => {
console.log('Chatting server is up and running.')
})
httpServer
.on('request', server)
.listen('3000', '127.0.0.1')
Which version of MongoDB are you using? Are you sending data to the broker?
Mongo version: v3.2.3. No, I just run the server and listen on the port.
@Emiya0306 can you send just some messages through?
And use Mongo 3.4?
I change Mongo version to v3.4.4, it still takes 59.5% CPU usage only listening. ~~I'm trying mqtt client send some messages, I think the result may be the same.~~
Oh, it's interesting! When I send some messages to server, the CPU usage is 0%. Restart the server is still 0% CPU usage. Then I delete the DB and start the server again, it will become high CPU usage. So how to avoid the first time high CPU usage?
This is a known pattern, given how little impacts production performance I never looked into it. Please send a PR if you find out.
same issue for me, the thing is, mosca is taking down the whole server. The clients must call a different http service before connecting to mqtt, but they can't since mosca is blocking the machine