jaymc Posted February 13, 2008 Share Posted February 13, 2008 I asked a question on here a few months ago about creating a chat system in php, mysql and ajax However, rather than check the database for new messages every 5 seconds to make it instant messaging, I want to have messages SENT to the client only when a new message was added This is the way MSN, ICQ etc work.. however, Because this is a web application relying on web technology I was a little stumped on how to do this I dont want to get involved with jabber or javascript sockets, can maybe be done in flash socket but again, not my field I was told you can use something called push. I have done a bit of research and found it is actually called comet. Here is the definition from wikipedia http://en.wikipedia.org/wiki/Comet_(programming) Does anyone know how to get this going? There is not much straight forward documentation on it regarding fully integrating and using it Hope someone can help! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/ Share on other sites More sharing options...
448191 Posted February 15, 2008 Share Posted February 15, 2008 I think you're in the wrong board. From what I read, it's just an Ajax techinque using either an IFRAME with indefinte length being filled with scripts, or using XmlHttpRequest. The concept is simple enough: make an embedded connection which doesn't timeout (on either side) and keep pushing data though it whenever it comes available. Try Ajax help. Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-467727 Share on other sites More sharing options...
jaymc Posted February 15, 2008 Author Share Posted February 15, 2008 Yeh but the point is, not having to continuously check a database for data, where as, when the data is available it is sent Thus elimination multiple requests per second That way, you truely could make a web based instant messenger/chatroom Its not ajax.. Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-467952 Share on other sites More sharing options...
448191 Posted February 15, 2008 Share Posted February 15, 2008 I'm starting to think you didn't read the Wikipedia page that you yourself posted. As Wikipedia explains, there are two methods (both of which are in fact Ajax techniques). Only one works in IE. http://softwareas.com/portable-comet-its-the-iframe-stupid You do not need any "Other Languages" only JavaScript and an IFRAME, hence: you're posting in the wrong board. Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468020 Share on other sites More sharing options...
jaymc Posted February 15, 2008 Author Share Posted February 15, 2008 Hmmm, that to me just prevents multiple http request Server side, I would need a php script looping database queries checking for new content right? And if found, send through the open comet tunnel Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468084 Share on other sites More sharing options...
448191 Posted February 16, 2008 Share Posted February 16, 2008 Hmmm, that to me just prevents multiple http request Which is the key to this "comet": keeping a single connection open and reading the output. You'd need a separate connection to "put" data from the client to the server. Server side, I would need a php script looping database queries checking for new content right? That's one approach, but it will cause a heavy load on your database server. I would recommend using a socket server. When a new message is received, write it to database but keep it available for requests. To do so you could, for example, use a simple queue for each socket. Don't worry about memory consumption, php doesn't make a copy of a value unless you're changing it. Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468248 Share on other sites More sharing options...
jaymc Posted February 16, 2008 Author Share Posted February 16, 2008 "When a new message is received, write it to database but keep it available for requests" So how is my script going to send it to the client without it checking a central database for new messages, also, why write anything to the database if its not being stored there due to it being sent down the tunnel in real time? Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468385 Share on other sites More sharing options...
448191 Posted February 16, 2008 Share Posted February 16, 2008 So how is my script going to send it to the client without it checking a central database for new messages, Look into socket servers. Basically a php socket server is a script stuck in an infinite loop, hence the data received from one socket will not be lost, and you will be able to write it to another, receiving socket. why write anything to the database if its not being stored there due to it being sent down the tunnel in real time? Just to maintain state. What happens if your socket daemon crashes? Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468428 Share on other sites More sharing options...
jaymc Posted February 16, 2008 Author Share Posted February 16, 2008 Yeh I know bits about socket servers, but only playing around with them through telnet just to demonstrate passing data about But I had to go through a port other than 80. This may cause a problem if Im pushing this out from port 81 or something What I invisage is something like this User 1 in his web browser SOCKET SERVER, SERVERIN User 1 and User 2 only in this session, aswell as other users User 2 in his web browser User 1 sends user 2 a message, the process of this is the message is sent to the socket server and pushed down the pipe to User 2 No mysql is involved in this assuming I remove the logging for security purposes This is the only way it can work, if it has to consecuatively probe the database, mission failed Can a PHP socket server do this, running on a single port yet serving multiple clients who communcate through a unique session assigned to those users only, so only thos 2 get the data and not everyone currently connected to the socket Hope that makes sense and hope you can advise Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468527 Share on other sites More sharing options...
448191 Posted February 17, 2008 Share Posted February 17, 2008 Yeh I know bits about socket servers, but only playing around with them through telnet just to demonstrate passing data about But I had to go through a port other than 80. This may cause a problem if Im pushing this out from port 81 or something You can use any port you like. On a server without root access you are restricted to the upper range though. It doesn't matter which port you use. User 1 in his web browser SOCKET SERVER, SERVERIN User 1 and User 2 only in this session, aswell as other users User 2 in his web browser Pardon? No mysql is involved in this assuming I remove the logging for security purposes No SQL needs to apply. Personally, I would write the messages to a database. A public chat system needs to be moderated. This is the only way it can work, if it has to consecuatively probe the database, mission failed Simply put, yes. It'll put too much load on the dbms and does not approximate real time like using a socket server. User 1 sends user 2 a message, the process of this is the message is sent to the socket server and pushed down the pipe to User 2 ... Can a PHP socket server do this, running on a single port yet serving multiple clients who communcate through a unique session assigned to those users only, so only thos 2 get the data and not everyone currently connected to the socket You can do this in two ways: the easy or the hard way. The hard way: make 'put' connections persistent (client push). You can keep track of which socket it talking to which other on the server. This requires the XHR connection to never time out. This may not be portable across browsers (if you can get it to work at all) and the page will appear to be continuously loading to the user. Simplified example: Loop checking for pending connections. User A connects to the server with message "I want to talk to User B". System will be putting all received messages from the connection of User A in User B's queue. Loop reading data from sockets. User A sends message: "Hi User B" Loop reads from User A's socket until some stop signal is received or the read operation times out. The message "Hi User B" is put in User B's message queue. Loop sends all connected receiving sockets the messages in their queue. User B receives message "Hi User B". User A disconnects and the association between User A and B is lost. send(data), method What about multiple send() invocations? Note that invoking send() doesn't change readyState directly which makes this tricky. If the readyState attribute has a value other than 1 (Open), an INVALID_STATE_ERR exception must be raised. ReadyState is unfortunately read-only, otherwise you would be able to do something like this: this.clientPush = function() { if(self.requestObject.readyState == 3){ self.requestObject.readyState = 1; self.sendNextPart(); } } I can offer you no solution for this. XHR is not designed for client push. Option 2 works the same, except a user has to reconnect and restate it's intention to talk to a specific user for each message. Instead of waiting for a stop message, the server simply reads until there is no more request data (or timeout). In this scenario (which would be easier to get working) the client keeps track of the other clients it's talking to and sends "Hey server! Send User B this message: 'Hi User B!'. Thanks.". Know what you're getting into. This isn't a Sunday afternoon job. Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468679 Share on other sites More sharing options...
jaymc Posted February 17, 2008 Author Share Posted February 17, 2008 Loop checking for pending connections. User A connects to the server with message "I want to talk to User B". System will be putting all received messages from the connection of User A in User B's queue. Loop reading data from sockets. User A sends message: "Hi User B" Loop reads from User A's socket until some stop signal is received or the read operation times out. The message "Hi User B" is put in User B's message queue. Loop sends all connected receiving sockets the messages in their queue. User B receives message "Hi User B". User A disconnects and the association between User A and B is lost. Yeh, it will be a lot of work and fiddling, this stuff isnt really documented much because hardly anyone uses it or has bothered to get it working successfully What I dont understand so far from your examples is how the socket creates a unique session between the 2 clients, for example user 1 connects to the session, the socket registeres a unique session of 8012431 user 1 then sends a PM to user 2 like this www.site.com/chat.php?id=8012431 user 2 clicks the link, connects to the socket himself but tells the socket he wants to be part of 8012431 session so he is binded through a private tunnel with user 1 They are both connected toa virtual pipe user 1 sends a message to user 2 stating he is part of 8012431 session the socket server says, ok I have 1 other person connected via 8012431 session, so he gets the message. The socket sends the message to anyone/user2 connected via 8012431 user 2 receives the message Can it work this way, if so, can you explain or provide some sample code of how a socket and bind 2 clients together through a session creating a virtual tunnel for them both to communicate down This appears to be the tricky part Quote Link to comment https://forums.phpfreaks.com/topic/90928-comet/#findComment-468775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.