5

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 ?

stefek143
  • 299
  • 5
  • 12
  • you are comparing a framework with chat script..if you would like to port the chat script over to node.js you should use socket.io since its almost an standard when mentioning websocket library in node.js. Also you should try to at least write the basic chat client in node.js using socket.io to really know which would be better. IMHO you should try it. – Gntem Mar 20 '14 at 08:09
  • 1
    The PHP example you point to is not WebSocket, but raw TCP sockets. You won't be able to connect with a browser to that. Here is a WebSocket server for PHP: http://socketo.me/. With Node, there are of course also multiple WebSocket server (and client) implementations. – oberstet Mar 20 '14 at 08:40
  • thx #oberstet for explain, but my chat is based on this tutorial: http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket , It's mean it's not "real" websocket? – stefek143 Mar 20 '14 at 08:50

2 Answers2

1

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!

Community
  • 1
  • 1
drnugent
  • 1,545
  • 9
  • 22
0

This is like asking "what's the difference between PHP and HTTP". Node.js is a technology framework you write code in. WebSockets is a protocol which can be implemented using a technology framework. You can use Node.js to implement the server-side aspect of WebSockets.