Jump to content

PHP, jQuery, comet. Need very basic tutorial to learn it.


shortysbest

Recommended Posts

I am trying to learn how to do comet based things for my website. It's a social network and I want to have as much realtime content as possible, IE. Comments, notifications, chats, etc. I have been looking for hours on google and such for some of these and I cannot find anything, well I found one that seems to work perfectly, yet it's in prototype and I work with jQuery and I cannot figure out how to adapt it to my needs, since I'm not programming with prototype, someone had converted it to jQuery but I couldn't get it to work for me, perhaps someone could help me adjust that?

 

if not maybe someone that knows about this could teach me about it, or point me into the direction to basic level tutorials and such.

 

The prototype one i was referring to earlier is:

 

http://www.zeitoun.net/articles/comet_and_php/start

 

(it's the 2nd example, labeled:Comet with classic AJAX : litte chat demo)

 

(the jquery attempt is in the comments)

 

Link to comment
Share on other sites

I would probably take a different approach on the problem at hand. Leveraging the power of node.js http://nodejs.org/

 

Install node.js on your server. After that create a simple http server with node.js which all your clients will connect to. This webserver would then keep a pool of all open connections (in other words people that are on your site). It would also be responsible for distributing any events that happen in your application to your clients. Create an api method that will only accept connections from localhost (or defined IP addresses with an API key, considering scalability). That API method simply proxies all stuff that it receives (from your PHP scripts) to your clients.

 

Inside PHP scripts. Create a class that that will send your events to the node.js http server ie $nodeJs->triggerEvent(new CommentEvent(stuff you need here));

 

Now the nodejs server will loop through your open connections and send the client a json object like { event : "comment", params : { "news_id" : 3232, "comment" : "Hey cool stuff" } } - This goes to your clients browser which has a javascript (could be jquery prototype or whatever js framework you're comfortable with) embbeded that receives this message from your server. Create a library that will modify your UI as you need it to. For this example your JS would try to find a comment box on the users browser -> if it finds one -> see if the comment box is assigned to the "news_id" news -> if it is -> add the comment "Hey cool stuff" to that comment box.

 

This was very trivial example but you should get the idea. Those examples that you have looked at seem so messy..

 

In short:

- Browser has a script that connects to your node.js server. (This script will also receive stuff and you can pass al the received events to your own library)

- PHP (or other language even) can trigger events by posting to your node.js server (Consider your node.js server as an observer that is notified by subjects and it will then do the stuff that needs to be done on certain events)

- Node.js just sits there between and propably JSON encodes the stuff coming from PHP (Save time for your PHP scripts, just push the events and the data needed for node.js, and do all the formatting there)

 

This way you can even narrow the events to be sent to a certain session (user) by just adding a param when you open your client connection. When you open the connection from browser, also send an identifier to node.js which will bind the specific IP address or connection to that ID. This ID should be generated by PHP so that you can refer to it over and over.

Link to comment
Share on other sites

Thank you very much, I know nodejs is the way to go for what I want to do, long polling is very ineffecient and would only be a temporary solution anyways. The problem is that I have been trying to figure out how to get nodejs working (installed, and then how to code it) but all of that is very new to me and I have been having troubles even installing it. (well I think it's installed, but idk how to check or anything) I haven't been able to find anyone that knows a lot about nodejs, and you seem like you know exactly how it all works. Do you think it would be at all possible for you to take a bit of your time to help me set everything up and write simple working examples so I know how to use it? That would be very much appreciated as my social networking site is in need of this technology. (I am going to private message)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.