Hello all,

We are trying to consume twitter Stream URL feeds (https://api.twitter.com/2/tweets/search/stream?tweet.fields=context_annotations&expansions=author_id) using Node.js Web-sockets (sockets.io). As mentioned in the documentation followed same steps like subscribing to rules URL (https://api.twitter.com/2/tweets/search/stream/rules) with defined rules of fetching the tweets stream from users which we follow i.e following list.

Everything seems setup correct as with few rules socket respond well with tweets results . But when we use this rules like this “from:twitterdev from:twitterapi has:links” as our requirement is to fetch tweets from the users which we follow . We are getting certain exceptions referring to Rate Limits, even though Bearer token account already has Elevated Access.

Please check this exception response :
{
title: ‘ConnectionException’,
detail: ‘This stream is currently at the maximum allowed connection limit.’,
connection_issue: ‘TooManyConnections’,
type: ‘Twitter API Response Codes & Error Support | Twitter Developer Platform
}

Does anyone have any ideas how we can we fix this blocker ?

Any help would be highly appreciated.

The rule from:twitterdev from:twitterapi has:links won’t work because a tweet can’t be from both accounts, a space is an implicit AND, so the rule should be (from:twitterdev OR from:twitterapi) has:links

As for why you have multiple connections, it depends on the implementation. Also remember you do not need to disconnect to change the rules. You can run the stream sonstantly in one worker / thread, and issue calls to the rules endpoint separately. Maybe in your code the conection is reconnecting after a rule change?

Thanks for this. I’ll try it out and let you know.

1 Like

thanks. still getting the error though. i’m really stumped.

here is my code:

const streamTweets = (socket, token) => {
let stream;

const config = {
url: streamURL,
headers: {
"User-Agent": "v2FilterStreamJS",
"Authorization": Bearer ${token}
},
timeout: 31000,
};
try {
const stream = request.get(config);
stream
.on("data", (data) => {
try {
const json = JSON.parse(data);
console.log(json);
if (json.connection_issue) {
socket.emit("error", json);
reconnect(stream, socket, token);
} else {
if (json.data) {
console.log(json);
socket.emit("tweet", json);
} else {
socket.emit("authError", json);
}
}
} catch (e) {
socket.emit("heartbeat");
}
})
.on("error", (error) => {
// Connection timed out
console.log("error", error);
socket.emit("error", errorMessage);
reconnect(stream, socket, token);
});
} catch (e) {
socket.emit("authError", authMessage);
}
};

I don’t really know javascript well enough to spot if there’s anything wrong or missing, but does an existing working example work for you? Twitter-API-v2-sample-code/filtered_stream.js at main · twitterdev/Twitter-API-v2-sample-code · GitHub