Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active December 15, 2022 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stephenlb/b6cadfd7c19964d6ccfc to your computer and use it in GitHub Desktop.
Save stephenlb/b6cadfd7c19964d6ccfc to your computer and use it in GitHub Desktop.
PubNub POST Gzip Examples
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]}');
#!/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/demo/demo/0/my_channel/0'
echo
import http.client
import zlib
def publish(msg):
req = http.client.HTTPConnection("pubsub.pubnub.com")
req.request("POST", "/publish/demo/demo/0/my_channel/0",
headers={"Content-Encoding": "gzip",
"Content-Type": "application/json; charset=UTF-8"},
body=zlib.compress(msg.encode()))
res = req.getresponse()
body = res.read()
print(body)
publish('{"hello":["world",123,null]}')
@pgl
Copy link

pgl commented May 13, 2014

PHP version:

https://gist.github.com/pgl/28bb140dbb8802b6da70

Requires the patch submitted as pull request 7: pubnub/php#7

@stephenlb
Copy link
Author

stephenlb commented Feb 26, 2020

#!/bin/bash

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## PubNub POST GZIP Example
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
echo '{"hello":["world",123,null]}' | gzip -c9 | \
curl \
    -H "Content-Encoding: gzip" \
    --resolve pubsub.pubnub.com:443:$(dig +short domain.server.pn) \
    --data-binary @- \
    'https://pubsub.pubnub.com/publish/demo/demo/0/my_channel/0'; \

echo

@stephenlb
Copy link
Author

stephenlb commented Mar 7, 2020

Large Message POST

MSG=`LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | fold -w 32700 | head -n 1`; \
echo "{\"msg\":\"${MSG}\"}" | \
curl -X POST --data @- 'https://pubsub.pubnub.com/publish/demo/demo/0/atari/0'; echo

HTTP/2

MSG=`LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | fold -w 32600 | head -n 1`; \
echo "{\"msg\":\"${MSG}\"}" | \
curl --http2 -X POST --resolve pubsub.pubnub.com:443:$(dig +short balancer-bronze-blue1.aws-pdx-1.ps.pn) --data @- 'https://pubsub.pubnub.com/publish/demo-36/demo-36/0/atari/0'; echo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment