Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

websocket php vs node js

Could somebody say what are the differences between "websocket php" http://www.php.net/manual/en/sockets.examples.php> and node.js? . I have chat with use websocket php but I don't know will better if this chat will move to node.js ?

like image 414
stefek143 Avatar asked Nov 10 '22 11:11

stefek143


1 Answers

Websockets are a transport built on TCP sockets. If you'll notice in the link you provided, the author recommends decoding data frames manually. There are projects that will help you with this, as @oberstet recommended (see also https://github.com/nicokaiser/php-websocket). @Kanaka has a great explanation for the difference between websockets and raw TCP sockets here.

Using node.js is certainly a smart, low-overhead way of rolling out a server for websocket connections. Another option is to use a realtime network. PubNub in particular has a blog post on how to write a chat app in 10 lines of code which is pretty accessible. Briefly, the code is:

Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>

Chat Output
<div id=box></div>

<script src=http://cdn.pubnub.com/pubnub.min.js></script>
<script>(function(){
    var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chat';
        PUBNUB.subscribe({
            channel : channel,
            callback : function(text) { box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML }
        });
        PUBNUB.bind( 'keyup', input, function(e) {
            (e.keyCode || e.charCode) === 13 && PUBNUB.publish({
            channel : channel, message : input.value, x : (input.value='')
        })
    } )
})()</script>

A simple approach like this will allow you to cut out the server hosting and configuration entirely. Hope this helps!

like image 86
drnugent Avatar answered Nov 14 '22 23:11

drnugent