Reputation: 299
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 ?
Upvotes: 5
Views: 6847
Reputation:
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.
Upvotes: 0
Reputation: 1545
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!
Upvotes: 1