node.js - create nodejs chat on php web application -


i've heard nodejs best choice creating real-time chat application. decide try one.

//on server side //create nodejs server var http = require('http');  var chatapp = http.createserver(function (request, response) {     //create html chat form listened nodejs server     response.writehead(200, {'content-type': 'text/html'});     response.write('<script src="http://localhost/nodejs/app/socket.io.js"></script><script src="http://localhost/nodejs/app/chat.client.js"></script><input type="text" id="message_input"><div id="chatlog"></div>');     response.end(); }).listen(8000); //create websocket protocol via socket.io var io = require('socket.io').listen(chatapp); //send data client io.sockets.on('connection', function(socket) {     socket.on('message_to_server', function(data) {         io.sockets.emit("message_to_client",{ message: data["message"] });     }); }); //on client side //get data server response var socketio = io.connect(); socketio.on("message_to_client", function(data) {     document.getelementbyid("chatlog").innerhtml = ("<hr/>" +     data['message'] + document.getelementbyid("chatlog").innerhtml); }); //submit , send data server via enter key document.onkeydown = function(e){     var keycode = (window.event) ? e.which : e.keycode;     if(keycode == 13){         var msg = document.getelementbyid("message_input").value;         socketio.emit("message_to_server", { message : msg});         document.getelementbyid("message_input").value = '';     } }; 

everything seems ok php webapp intergration. how make work part of php web page?

as mentioned in original comment, can let php application continue doing has been doing along , use nodejs handling web socket connections (via socket.io) library. here example of simplified structure use:

your chat.php page or chat controller:

<?php // handle /chat route // perform authentication database // render template ?> <!-- following html rendered --> <html>   <head>     ...     <script src="http://localhost/nodejs/app/socket.io.js"></script>     <script src="http://localhost/nodejs/app/chat.client.js"></script>   </head>   <body>     ...     <input type="text" id="message_input">     <div id="chatlog"></div>     ...     <script>     var socketio = io.connect('http://localhost:8080');     socketio.on("message_to_client", function(data) {         document.getelementbyid("chatlog").innerhtml = ("<hr/>" +         data['message'] + document.getelementbyid("chatlog").innerhtml);     });     //submit , send data server via enter key     document.onkeydown = function(e){         var keycode = (window.event) ? e.which : e.keycode;         if(keycode == 13){             var msg = document.getelementbyid("message_input").value;             socketio.emit("message_to_server", { message : msg});             document.getelementbyid("message_input").value = '';         }     };     </script>   </body> </html> 

your nodejs application following. note lack of regular http connection handling, let php handle:

//create websocket protocol via socket.io var io = require('socket.io').listen(8080); //send data client io.sockets.on('connection', function(socket) {   socket.on('message_to_server', function(data) {     io.sockets.emit("message_to_client",{ message: data["message"] });   }); }); 

and base use. mentioned before in comment, possible extend add database-backed authentication mechanisms nodejs portion of server.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -