Question

I'm creating an application that has a chat feature, and I decided to use NodeJS / socket.io for this. My project also uses PHP, and I want to store data from my application in a MySQL server. This is what currently happens

socket.on (client connect) -> AJAX script that adds the new client's username to a MySQL database (I want to keep a list of users connected to the chat in MySQL)

anyone sends a message -> AJAX script puts a timestamped version of that message into a MySQL database (Chat log)

socket.off -> AJAX scripts removes the client from the MySQL database

Am I using so much AJAX that I am ruining the benefit of using socket.io? To my understanding: I am still getting the benefit of real-time connections with socket.io, and my AJAX is causing a separate load on my PHP server, but these AJAX actions shouldn't interrupt or block Node. But will this separate load on the php server slow down the client-side socket.io connection? I am new to Node / socket.io so I may be missing some fundamental knowledge about what's going on in the back-end.

No correct solution

OTHER TIPS

That's not going to ruin websockets, but clearly it is an unnecessary overload of the server. Why do you use AJAX? Why don't you update the database directly from Node??
Those AJAX calls are going to hit the server permanently and will sure cause trouble. One of the reasons to use websockets (and also long-polling) is to avoid that excessive load. If both PHP and Node.js are on the same server, excessive load would slow down both.

When a user (aka socket) connects/disconnects, Node.js can add/remove that user to MySQL. If the Node.js server is on a different machine, allow MySQL remote connections for that separate server.
And the same with messages, update the database directly from your Node.js script.

Here there is a Node driver for MySQL: https://www.npmjs.org/package/mysql

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top