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 ?

3
  • 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. Commented Mar 20, 2014 at 8: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: socketo.me. With Node, there are of course also multiple WebSocket server (and client) implementations. Commented Mar 20, 2014 at 8:40
  • thx #oberstet for explain, but my chat is based on this tutorial: sanwebe.com/2013/05/chat-using-websocket-php-socket , It's mean it's not "real" websocket? Commented Mar 20, 2014 at 8:50

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.