Last active
December 15, 2022 21:19
-
-
Save stephenlb/b6cadfd7c19964d6ccfc to your computer and use it in GitHub Desktop.
PubNub POST Gzip Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}') |
#!/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
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
PHP version:
https://gist.github.com/pgl/28bb140dbb8802b6da70
Requires the patch submitted as pull request 7: pubnub/php#7