I'm trying to achieve a notification system using NodeJS. Everything is going right: connecting to server, connecting to database, querying the database. Since I have just couple of hours working on NodeJS, I don't have the image clear how the notifications concretely happen. Googeling the issue didn't come actually with great things.
When someone posts a comment, the comment's inserted to the database, that should fire an event to notify his friends (in Friend table) that he posted a comment? Any type of help will be appreciated.
I'm using PHP and MySQL with "mysql" Node module.
Googleing "node.js notifications" I got this http://www.gianlucaguarini.com/blog/?p=272 or this http://gonzalo123.wordpress.com/2011/03/14/real-time-notifications-with-php/. It uses socket.io as Amberlamps suggests.
In the end you will want to have all of your users (their webbrowsers) run some javascript that opens a websocket (or otherwise, check out Socket.IO) to your node.js server. That connection stays open as long as they stay on the page. Once you want to send them a notification the node.js server does that through this connection. Your PHP script can trigger the node.js server through various ways, but a http request to localhost is probably the easiest and most realtime.
Related
I recently need to work on a project which involves having a chat. This chat must update in real-time and it is estimated to be used by more than 9000 users at the same time. I have done some researching on how to do that and came to a conclusion: Use ajax
While I researched on ajax, I found a problem:
Problem 1:
If there are a lot of users where the browser is constantly creating ajax call for a file to get the database chat content, wouldn't that put a lot of strain on the server and eventually won't it collapse?
There are a lot of libraries out there which maybe can fullfil my needs but I wanted to start from scratch, is it possible?
Take an example, whatsapp: if you open dev tools you don't see it making ajax calls but when I receive messages, it also doesn't makes the call. facebook on the other hand will get ajax call when users receive a message.
PS: I am not looking for the code, I just want a way to do it. I can code it myself. (I am using php with mysqli)
You'll need to utilize WebSockets: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API
This allows the browser to keep a connection open with the server for constant communication, both to and from the server.
The alternative is polling, which is sending periodic ajax requests, as you described.
From the Mozilla page:
With this API, you can send messages to a server and receive
event-driven responses without having to poll the server for a reply.
I'm creating a mobile app with a server backend that will authenticate a user and continuously send them updates whilst listening for post data from the mobile app. These updates will be specific to the person, pulled from a database.
From my research it seems that I should use a websocket. I'm familiar with PHP so have tried Ratchet. I've created a simple chat script with Ratchet which queries a database onMessage and sends the data to the client.
My question is, are websockets right for this? When a server receives a connection it must query the db every 5 seconds and send updated info to the app. It must listen for messages that will change the db query. Everything in Ratchet's docs seems to be focussed on subscriptions to topics rather than treating each client individually, although I've gotten around this by using:
$client = $this->clients[$from->resourceId];
$client->send("whatever_message"):
Am I complicating things by using Ratchet? Or should I use a child process to handle each client?
I am sorry for a vague questions. I've researched as best I can but cannot establish whether I'm heading in the wrong direction! Thank you for any help.
That is a good formula. Sending post data from the apps while maintaining a socket connection is a good distribution of processes. However PHP might not be your best option for running the socket server.
The reason for this is PHP is a single threaded language which doesn't sport an elegant event system.
Take NodeJs as an alternative. It too is single threaded, however you can register events on socket servers allowing the software to run additional control processes while it waits for network activity.
This does not limit you to javascript however. Work can still be delegated to PHP processes from the NodeJs application (I use NodeJs as an example only, there are other options such as Java, Python, or good ol' native).
For moving work to PHP, you can either execute commands, or use a job server to enable synchronous and asynchronous tasks.
Here are a few resources you can combine to accomplish this:
http://nodejs.org/
http://socket.io/
http://gearman.org/
http://php.net/manual/en/book.gearman.php
And if you are using Symfony:
https://github.com/mmoreram/GearmanBundle
I am designing an app that will have to periodically check a web address for updates.
The updates will be in a MySQL database tables.
I know the idea way to do this, is to create a service that is constantly running and trying to check for an update periodically (lets say 10 seconds).
Below are some questions that are unclear to me as I start my quest to accomplish this task.
Do I need to manually do the check for an update everytime from the client to server ? (that is, take a value on the client side, send it to server side, and do a head to head comparison), OR, the php/MySQL server can send a notification that there is an update that took place.
I came across a RSS feature in several posts in SO, however those tend to be NEWS applications, mine is not anything like that. Does it help to use RSS feeds in here ?
I'm planning on doing the check every 10 seconds. This means that upon the install of the app and since the app is on the device (and as long as there is internet connectivity) I will keep on fetching the server. This tends to be bandwidth and ram/cpu consuming for the client side. How do big apps like Viber, WhatsApp manage to do so ?
Just thinking out loud - , in order to avoid such a hassle, I was thinking with each update on the server, send a notification to the user with a certain code, and do the math inside the onReceive, if the code was something related to an update, 1-not show the notification to the user, 2-run the server update check thing.
ideas ?
The trouble is traditional php/html systems are client centric (the client has to initiate all communications).
You might want to look into websockets, or node.js. These technologies will allow you to create a persistent connection between the server and its clients. This in turn will allow you to 'push' data to the client when appropriate, without the client having to ask for it every x amount of minutes.
Node.js Chat Example
Node.js info
How to get a information on the client side, without requesting the database?
The example would be a simple chat application. Two clients logged to a stream, like chat room. One filling a form, sending information by ajax to the database. Tthe other one gets it without requesting, like pushed from a event listener from the database.
Is that possible?
Many thanks.
It is possible to implement non-blocking I/O with PHP in a similar vein as nodejs. see: http://reactphp.org/
I would still say PHP is probably not the right tool for the job if you're just looking to make a realtime chat app. This is what nodejs excels at.
The HTTP layer doesn't support what you wish to accomplish. You can find more information about this in this Stackoverflow page.
You might want to use node.js and socket.io. You can also try this tutorial about creating a chat system with node.js and socket.io.
You can pass information in-memory in server like Node.js.
Chat message would be uploaded using AJAX POST and distributed to other clients via active SSE connections (you'd have to keep track of them, e.g. in an array).
However, without a database you don't have persistency of messages. That's fine if it's OK to lose messages when clients disconnect, but in case of a chat users may expect to receive backlog of messages sent while they were offline.
I have a simple php page that displays data from a mysql database. I want it to update itself automatically when data gets changed on the server. (I don't want to update the page periodically at fix time intervals.) I guess I need the technology behind FB chat box or omegle. But I don't know how to implement it on php and mysql. I would be grateful if you could help me. Thanks.
You would need to look into WebSockets or a Comet server (which uses long-polling techniques) to accomplish a push system. Alternatively, instead of using push-like notification, you could make frequent polls to the server with nothing more than a request identifier and a timestamp, let the server decide if there is anything new since the last poll, and serve up the data if there is.
you can implement Comet technology which is opposite of Ajax. JavaScript Dojo Toolkit can be useful to handle this method well.
Dojo WebSocket
http://dojotoolkit.org/features/1.6/dojo-websocket
http://cometd.org/
"Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it."
http://en.wikipedia.org/wiki/Comet_(programming))