I have created template for user to user chat module. Need help to complete realtime chat.
Need some validations like highlight chat window on message received if it is inactive.

to_id is collected in script (var userid). how to collect from_id , if user is logged in via php. currently there is no login, but i will add login script later.
How to get session id or login id in javascript



CREATE TABLE `chat_interactions` (
`message_id` bigint(11) NOT NULL,
`to_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`from_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`message` text COLLATE utf8_unicode_ci NOT NULL,
`ip_address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `chat_interactions`
ADD PRIMARY KEY (`message_id`);
ALTER TABLE `chat_interactions`
MODIFY `message_id` bigint(11) NOT NULL AUTO_INCREMENT;
10 Months
Discussion Span
Last Post by jkon

Looks like you are trying to do something much harder than you can chew. A 1-to-1 chat implementation isn't trivia. Also, you seem to lack simple knowledges on session... You can't take other people's code without clearly understanding your own purpose first.

If you are using PHP as your backend handler, the from_id should be from your PHP session. In other words, it is the logged in user ID which should be saved in the log session. If you implement the authentication system yourself, you should know where and how you save the ID in the session. If you don't, then you need to look for how session values are saved.

Anyway, I still do not have a clear picture of what you really want to do here. You need to be more specific and show how you implement it. Can't really give you more specific answer.


I have created login script, - $_SESSION["user_id"] = $row[user_id]; - contains logged in user ID. $user_id = $_SESSION["user_id"]; & for username $_SESSION["user_name"]

I have created template as on jsfiddle. I have problem with websocket. My template gives to_id in jQuery how to collect session in jQuery (it's from_id )

I have created 1 to 1 private message using php which is not real time & not based on jQuery/javascript.

Now I'm working on realtime 1 to 1 chat . I'm trying to make it like gmail chat.

e.g. mail.google.com

Inside gmail account, chat app allows you to chat with multiple users at same time, in real time. If you are chatting with only one person & at the same time if second user sends you message another chat window pops showing message received from another user. I have created almost same template using jQuery.

I used this WebSocket tutorial. But it closes connection immediately after connecting .

I hvae two pages send_msg.php to send chat message in real time & get_msg.php to receive message in real time

Need help to make it real time using websocket. (I'm trying to use PHP: Sockets)

Thanks for your reply


Hello hrushi9,
Your original question about MySQL and WebSockets only proves the phrase “Looks like you are trying to do something much harder than you can chew.” that Taywin used. Currently there isn't any open source framework (in my knowledge) in PHP that make WebSockets work in a simple but secure way. My company has created such a framework (as C++/C extension in PHP) but we hesitate to make it public and one reason for that is that we can't underline enough the simple statement “don't use WebSockets if you are not willing to pay time to understand what is happening” , even at basic level (the framework handles all the others).

What we have till here. Most use Ratchet http://socketo.me/ . I find it very good for elementary understanding WS the project https://code.google.com/archive/p/php-websocket-server/ .

Don't get me wrong I don't suggest that you shouldn't engaged with WS (in my opinion WS will be the core of future web apps) but till now we have nothing simple and secure for someone that is not willing to take a lot of time reading , understanding , testing (I hope that you are not of those) . Maybe in the future something will be released that makes things simpler and safer but again you should pay time understanding or else a real disaster could happen.

Edited by jkon


I second the use of Ratchet - that was the only method for PHP I tried that worked. Even that gave me a bit of a nosebleed.


I admire the people that created Ratchet and gave it to the community , because they tried and maybe it is the best published PHP tool for WS that we have till now. I haven't used it but I studied it before taking the decision to move our framework from PHP to a PHP C++/C extension. The reason was to find out if the do something different and solve the problems we have with PHP and WS. They don't and moreover I find it not making things simpler and safer. But again , it may be the best published PHP tool for WS that we have till now.

The main problem with PHP and WS in my opinion is in the core of PHP , it is how PHP execute the orders sequentially. This makes the use of WS really problematic in real word apps (of course in examples everything can work great). Think for a example a chat app in production that it has only 300 users online , that “talk” in several channels and exchange private messages , think what sequential execution in the WS PHP server means , and how awful and buggy result you would have. The core of the solution to this (plus a different architecture approach) is if you had threads.

PHP has pthreads that is great for what it is. An extension for batch PHP files with a restricted OOP usage (because it is true that giving the lock and the unlock option in properties level would probably resulted in disastrous PHP code implementations of it). So having threads that we can use with unrestricted OOP but safe way would allow us to create a whole different architectural approach of a WS server in PHP and that is why we moved to a PHP C++/C extension. We are far for making it public and I know that few would care (you should have enabled zts in your PHP configuration when you installed it , then you should install that as an extension , and finally get the time to learn how to use it and understand even the basics)

Edited by jkon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.