The community forums are in BETA and closed to the public.

Publishing Data Chunks via Streaming POST TCP and Compression with GZIP

Publishing Data Chunks via Streaming POST TCP and Compression with GZIP

You may have larger messages which are compressible payloads including large JSON blobs or base64 encoded binary objects or chunks. Compressing your payloads saves on bandwidth especially in high volume data transfers. The following are quick and easy examples of sending publishing GZIP payloads.

Basic POST Example

#!/bin/bash

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## PubNub POST GZIP Example
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
echo '{"hello":["world",123,null]}' | \
curl \
    --data-binary @- \
    'http://pubsub.pubnub.com/publish/PUBKEY/SUBKEY/0/CHAN/0'
echo

This is a bash simple-to-grasp example which excepts STDIN standard input of any text or file.

(gzipped) Publish POST w/ GZIP Example in Bash

#!/bin/bash

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## PubNub POST GZIP Example
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
echo '{"hello":["world",123,null]}' | gzip -c9 | \
curl \
    -H "Content-Encoding: gzip" \
    --data-binary @- \
    'http://pubsub.pubnub.com/publish/PUBKEY/SUBKEY/0/CHAN/0'

echo

This is a bash simple-to-grasp example which excepts STDIN standard input of any text or file.

Next we'll show you a Node.JS example which is surprisingly more complicated than bash example.

(node.js) Publish POST w/ GZIP Example in Node.JS

var http = require("http");
var zlib = require("zlib");

exports.publish = function(msg) {
    var req = http.request({
        "host" : "pubsub.pubnub.com",
        "method" : "POST",
        "path" : "/publish/demo/demo/0/my_channel/0",
        "headers" : {
            "Content-Encoding" : "gzip",
            "Content-Type" : "application/json; charset=UTF-8",
        },
    }, function (res) {
        var body = "";
        res.on("data", function (chunk) {
            body += chunk;
        });
        res.on("end", function () {
            console.log(body)
        })
    });

    req.on("error", function (e) {
        console.error("ERROR:", e.message);
    });

    zlib.gzip(msg, function (error, result) {
        console.log("data:", result);
        req.write(result);
        req.end();
    });
};

exports.publish('{"hello":["world",123,null]}');

Comments to this discussion are now closed!