Socket.io: Support for WebSocket, a framework for web-side instant Messaging

Source: Internet
Author: User
Tags emit

first, the basic introduction

WebSocket is a new communication protocol for HTML5, which realizes two-way communication between the browser and the server. The Socket.io is a fully JavaScript-based, node. js, WebSocket-enabled protocol for real-time communication, a cross-platform, open source framework that includes both the client's JavaScript and the server-side node. js.

In addition to supporting the WebSocket communication protocol, Socket.io supports many kinds of polling (Polling) mechanisms as well as other real-time communication methods, and encapsulates the generic interface, and implements the corresponding code for these real-time mechanisms on the server side. The polling communication mechanism implemented by Socket.io includes Adobe Flash sockets, Ajax long polling, Ajax multipart streaming, persistent iframe, JSONP polling, and more. Socket.io can automatically choose the best way to implement network real-time application according to the browser's support to communication mechanism. Currently, the latest version of Socket.io is released on January 19, 2015 in version 1.3, version 0, which enhances stability and improves performance, and fixes a number of bugs.

The goal of the Socket.io design is to build real-time applications that work well on different browsers and mobile devices, such as real-time analytics systems, binary streaming data processing applications, online chat rooms, online customer service systems, commenting systems, Webim, and more. Currently, Socket.io already supports mainstream PC browsers (ie, Safari, Chrome, Firefox, opera, etc.) and browsers on mobile platforms (Safari under the iOS platform, WebKit-based browsers under Android).

Socket.io already has many powerful modules and extension APIs, such as (Session.socket.io) (HTTP session middleware, session related operations), Socket.io-cookie (Cookie parsing middleware), Session-web-sockets (passes the session in a secure manner), Socket-logger (the JSON-formatted logging tool), WebSocket. MQ (Reliable Message Queuing), Socket.io-mongo (adapter with MongoDB), Socket.io-redis (Redis adapter), Socket.io-parser (Default protocol implementation module for server and client communication), etc.

Socket.io realizes real-time, bidirectional, event-based communication mechanism, which solves real-time communication problems and unifies the programming mode of server and client. After you start the socket, it's like building a client-to-server pipeline with both sides. It can also be a good combination with the traditional request method provided by Express.js, that is, the same domain name, the same port can provide two kinds of connection methods.

Second, a simple example

Building a Socket.io environment requires creating a directory as a workspace, then installing node. js, and installing Socket.io (command: NPM install Socket.io) in the workspace so that the environment is built. You can also install server-side development that is based on the node. JS framework (such as express.js, etc.) to assist your application.

The server segment sample code is as follows:

varIO = require ('Socket.io'). Listen (8080); Io.sockets.on ('Connection', function (socket) {Socket.emit ('News', {hello:' World' }); Socket.on ('My other event', function (data) {Console.log (data); });});

The client sample code is as follows:

<script src="Https://cdn.bootcss.com/socket.io/2.1.1/socket.io.dev.js"></script><script type="Text/javascript">varSocket = Io.connect ('http://localhost:8080'); Socket.on ('News', function (data) {Console.log (data); Socket.emit ('My other event', {my:'Data' });});</script>

After execution: Client accepts server push message news, print {Hello:world}

After the client sends information to the server, triggering the server-side event, printing {my: ' Data '}

Socket.io: Support for WebSocket, a framework for web-side instant Messaging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.