简体   繁体   中英

HTML5 websockets vs PHP websockets vs node.js websockets?

I have decided to use WebSockets for my website chat application and I have just started to learn websockets, but I have three different options, node.js , PHP or HTML5.

What I want to know that is there any difference between the three, I mean I don't want to learn all the three if any one is better than the others.

Web Sockets is a protocol which defines how two parties can communicate. It's language agnostic; any language can provide an adapter to talk to another web socket. The three things you mention are three different implementations for of this adapter. For a chat application, you'll likely need at least two of them: one server, one client. Choose which language you'd like to write your server in (PHP or Node.js) and use the HTML 5 web sockets feature in the browser to talk to the server.

What are websockets:

WebSockets represent a standard for bi-directional realtime communication between servers and clients. between any server and any client.

A WebSocket server can be written in any server-side programming language that is capable of Berkeley sockets, such as PHP or Node.

So like any other protocol, lets say like http: you will need a client and a server.

As already mentioned, server side implementations for web-sockets can be done using any one of the server side languages, including PHP and Node.

And for the client side implementation you will need html5 we-bsockets, which run on the browser.

So of-cource you will not need to learn both PHP and Node at the same time, but one of these plus html5.

I would recommend using NodeJS for implementing sockets, as it will let you use same technology(JavaScript) on both client and server. There is socket.io module for nodeJS which will take care of both server-side and client-side components with similar APIs.

So you will not require to write too much of code for implementing sockets. here is an example with node:

Server:

var server = require("net").createServer();
var io = require("socket.io")(server);

var handleClient = function (socket) {
    // we've got a client connection
    socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};

io.on("connection", handleClient);

server.listen(8080);

Client:

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect("http://localhost");
</script>


socket.on("connect", function () {
    console.log("Connected!");
});

Note:

web-sockets for client side is html5 feature, and not availbale in browsers which don't support html5. You can see the browser support here

If you still choose PHP you can learn about HTML5 websockets here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM