资讯专栏INFORMATION COLUMN

REST Streaming

sarva / 3286人阅读

1.Use REST streaming to connect products directly to nest services from cloud to cloud integrations.

2.In REST streaming, instead of returning information and close the connection, the NEST api keeps the connection open, and the REST api uses the open connection, and users can receive new state.

3.REST streaming allows for only one token(one user) per socket.

4.What is SSE(server-sent-events): a web app subscribes to a stream of updates generated by server, and whenever a new event occurs, it will send to the client.
AJAX:

       - Polling: the application repeatedly polls server for data.
       - Long Polling: the server does not have the data available, the server holds the data until new data is made available(Hanging GET). 

5.server-sent events are sent over http, but they do not require a special protocol or server implementation get working. When use server-sent requests, a server can push data to client whenever it wants, without making initial request, update can be streamed to clients as they happen. SSEs often use single un-directional channel between client and server.
Diff between SSE and Longpolling is: SSE is handled by browser and users have to listen for messages.

Server Sent Events vs Websockets:

websocket provides richer protocol to perform bi-directional, full-duplex communication, two way channel for games, messaging apps, and for cases when need real-time communications for both directions.

Server Sent Events: sometimes not needed to perform data sent from the client. Only need is to update the event from the server side.

//JavaScript API:
    if(!!window.eventSource) {   
        var source = new EventSource("stream.php");
    } else {
        //result to xhr polling;
    }
    //set up a handler for the message event
    source.addEventListener("message", function(e) {
        console.log(e.data);
    }, false);
    source.addEventListener("open", function(e) {
        //connection is opened
    }, false);
    source.addEventListener("error", function(e) {
        if(e.readyState == EventSource.CLOSED) {
           //connection is closed;
        }
    }, false);

Server implementation in NodeJS:

var http = require("http");
var sys = require("sys");
var fs = require("fs");

http.createServer(function(req, res) {
    if(req.headers.accept && req.headers.accept == "text/event-stream") {
        if(req.url == "/events") {
            sendSSE(req, res);
        } else {
            res.writeHead(200, {"Content-Type": "text/html"});
            res.write(fs.readFileSync(__dirname + "/sse-node/html"));
            res.end();
        }
    }).listen(8000);

function.sendSSE(req, res) {
    res.writeHead(200, {
        "Content-Type":"text/event-stream",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive"
    });
    
    var id = (new Date()).toLocaleTimeString();
    
    //send a SSE every 5 seconds on a single connection
    setInterval(function() {
        constrcutSSE(res, id, (new Date()).toLocaleTimeString());
}

function constructSSE(res, id, data) {
    res.write("id: " + id + "
");
    res.write("data: " + data + "

");
}

function debugHeaders(req) {
    sys.put("URL: " + req.url);
    for(var key in req.headers) {
        sys.put(key + ": " + req.headers[key]);
    }
    sys.put("

");
}
    
        

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/108054.html

相关文章

  • Flink1.7稳定版发布:新增功能为企业生产带来哪些好处

    摘要:通过状态演变,可以在状态模式中添加或删除列,以便更改应用程序部署后应捕获的业务功能。本地恢复通过扩展的调度来完成本地恢复功能,以便在恢复时考虑先前的部署位置。此功能大大提高了恢复速度。问题导读1.Flink1.7开始支持Scala哪个版本?2.Flink1.7状态演变在实际生产中有什么好处?3.支持SQL/Table API中的富集连接可以做那些事情?4.Flink1.7新增了哪些连接器Ap...

    Hwg 评论0 收藏0

发表评论

0条评论

sarva

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<