Background
I was working on a StackOverflow helper program (that sends me notifications) when I noticed something rather interesting in StackOverflow's Network tab:
StackOverflow uses WebSockets (not too surprising, but good to know)! For this challenge, we will be conquering StackOverflow's web socket server.
This particular method could be transferred over to other domains related to StackExchange, but StackOverflow is very popular and a good example. Feel free to lookup tags for other domains on StackExchange (feel free to connect to those instead, although I doubt it would reduce byte count since StackOverflow has id 1).
StackOverflow's Unofficial WebSocket API
There are two separate WebSocket servers you could use:
- The secure,
wss://qa.sockets.stackexchange.com/
- Or the insecure,
ws://qa.sockets.stackexchange.com/
Since we are code golfing and we aren't going to be sending any not-already-public-information, I suspect people will lean more toward the insecure option.
Next, in order to subscribe to our favorite tag, we need to send the following to the WebSocket to 'subscribe' to the tag's feed.
1-questions-newest-tag-javascript
You can replace javascript
with any tag we're interested in.
The server will occasional send heartbeat requests. You must reply with hb
(so you can keep the connection alive).
A heartbeat requests looks as follows:
{"action":"hb","data":"hb"}`
Here's a basic JavaScript-based socket (it may take a several minutes for there to be a new post, depending on time of day). I used the most popular tag JavaScript to test with, but if you know of a more popular tag, go ahead and leave a comment.
Example:
var exampleSocket = new WebSocket("wss://qa.sockets.stackexchange.com/");
exampleSocket.onopen = function (event) {
exampleSocket.send("1-questions-newest-tag-javascript");
};
exampleSocket.onmessage = function (event) {
if (JSON.parse(event.data).data === "hb") {
exampleSocket.send("hb")
}
console.log(event.data)
};
Specifications
Input: Any tag the user wants to receive updates about.
Output: Should be the first question (from when you connected to the WebSocket) and all consecutive available for said tag on StackOverflow.
Rules:
- You must use WebSockets. Or, at least connect to
sockets.stackexchange.com
(including,ws://qa.sockets.stackexchange.com
) and use it to receive updates for the inputted tag. If you can find a better way of doing it (like not using the1-questions-newest-tag-<tag>
but still receive updates fo the inputted tag) by all means. Or if you can write your own WebSocket equivalent in less bytes, even cooler. - Least number of bytes wins!
Language Compatibility
External libraries are encouraged. Many programming languages have a compatible library.
data
field? \$\endgroup\$