问题
Currently, i have created an application in node.js to send notification to all client using socket.io with mysql. So any change in my mysql database will be notified to the multiply clients through socket.io instantly. The application work charm.
The problem i'm facing is related to performance issue.
My browser get the constant push notification from the server, which slows down the performance of my browser. I/Client need to get the notification only on the database change, rest of the time the Server should be in pause state, with out sending any message to cilent.
In other words i need to implement long polling technique.
How do i implement long polling with socket.io?
I have googled to look for an example, but i found none which could help me to implement long polling with socket.io and mysql in node.js
Is it possible to implement long polling with socket.io?
If so, could somebody redirect me to some useful link with example?
Thanks in advance.
回答1:
You can force socket.io to use only long-polling:
io.set('transports', ['jsonp-polling']);
But I don't think long-polling is the solution you are looking - for. You should be independet from the underlying technique used. I might do something like this:
function writeToMysqlDb(data) {
mySqlDriver.write(data); //whatever you use to write data to MySql
socket.emit('dbChanged', data); //send the same data to all connected clients
};
I would leave the transports to the default options: "transports defaults to websocket, htmlfile, xhr-polling, jsonp-polling"
回答2:
you can build a pool to merge the constant push notification in one notification every second to reduce push count.
来源:https://stackoverflow.com/questions/20698242/how-to-implement-long-polling-with-socket-io