Using javascript to check if m3u8 live stream is active - javascript

Issue
I have a TVML app which calls a m3u8 live stream. The problem is that if the stream isn’t active, the video player throws an error message which cannot be customized. What I want to do is to check if the stream is live before attempting to play it. Is there a way I can accomplish this in javascript?
Error:
(update)
the TVJS playbackError event returns an IKJSError with code -1008, description: resource unavailable and domain: NSUrlErrorDomain. This Error also has an underlyingError (also IKJSError) with code -12884, description: “The operation couldn't be completed. (CoreMediaErrorDomain error -12884 - Playlist File not received)”.
The problem is that when this error is received the player immediately shows an error message on the TV which I can't replace. This is why I want to check if stream is live before attempting to play it.

Use this:
async function checkHLSActive(url) {
try {
let res = await axios.head(url);
return /2\d\d/.test('' + res.status);
} catch (err) {
console.log('err', url, err);
return false;
}
}
Don't forget to import axios library for wherever you put this.

Related

SignalR error when invoking method on the server from JavaScript client

I have a C# server running my hub class, which contains only 1 method in there, which is as follows,
public class HothHub : Hub
{
public async Task AddSingleUserGroup(string name)
{
await Groups.AddToGroupAsync(Context.ConnectionId, name);
}
}
I also have a JavaScript client, which connects to the hub via the following code,
var connection;
async function signalRStart() {
connection = new signalR.HubConnectionBuilder()
.withUrl("https://somesignalrurl.com/hothhub", { withCredentials: false })
.withAutomaticReconnect()
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on("hothHubToHothUpdate", () => {
console.log("Called by the server!");
});
connection.onreconnecting(error => {
console.log("Connection lost due to error " + error + ". Reconnecting.");
});
// Start the connection.
await start();
}
async function start() {
try {
await connection.start();
connection.invoke("addSingleUserGroup", "someUniqueUserName");
} catch (err) {
console.log(err);
setTimeout(start, 5000);
}
};
Now when the client initiates the connections and run start() on itself, this part seems to run fine. A connection to the signalR hub is made successfully. The problem I'm having is when connection.invoke("addSingleUserGroup", "someUniqueUserName"); is run although the error does not happen all the time. On first run, the method at the server end is hit successfully however, it looks like subsequent calls to it fail and this is the error returned in the client,
Uncaught (in promise) Error: Failed to invoke 'addSingleUserGroup' due to an error on the server. HubException: Method does not exist.
at _callbacks.<computed> (signalr.js:1252:36)
at HubConnection._processIncomingData (signalr.js:1364:33)
at HubConnection.connection.onreceive (signalr.js:985:52)
at webSocket.onmessage (signalr.js:2236:30)
I've read a few articles on here but most seemed to be related to the client calling the wrong method name having used a capital letter at the start of the method name when invoking it and some having mentioned issues with the method expecting 1 type parameter and receiving another type although in my instance here its hard to think how the server would not treat the incoming parameter as a string, which is what is being passed in. Has anyone got any ideas on what could be wrong here or what I could try?
Thanks!
Unfortunately I dont have an actual answer for this but after deploying the solution to my Azure App Service, the release version does not produce the error. It seems the error only persisted when in debug mode but like I said I'am not sure why.

videojs - How to catch custom http error codes during live stream?

I have a live stream (HLS) that I am playing using videojs 6.8. For some users, after the playback has started (about 4-5 .ts files have been loaded) the server throws me a 409 error.
How do I catch this specific error code so that I can programmatically stop the playback and show an error message?
Currently, videojs keeps trying to resume playback indefinitely. I have tried retryplaylist, blacklistplaylist but all the info I get is that the playlist has been blacklisted and is being retried, I do not see the HTTP code anywhere in my console.log(). player.on('error') doesn't throw any error. I have tried all three of the following but none of them gives me the http code:
player.on('error', function (e) {
// no log
console.log(e);
})
player.tech().on('retryplaylist', function (e, data) {
// logs that it is being retried, but no http code
console.log('retry');
console.log(e);
})
player.tech().on('usage', function (e, data) {
// logs the even 'retryplaylist` but does not give me a http code.
console.log('usage');
console.log(e)
})
I do not want to put my message in retryplaylist because that event will be thrown in case of a slow network too (I already tested this).
What do I have to do to catch the specific 409 error?
Look like:
var retries = 0;
player.tech().on('retryplaylist', function (e, data) {
retries++;
if (retries >= 3) {
// do something
}
})

Twitter Bot Node.js and Twit Package

PROBLEM
So im trying to create a twitter bot and everything was going fine until i tried to auto reply to users who follow me.
I'am learning and was watching from this tutorial Coding Train Twitter Bot(LINK) but i seem to get this error(PHOTO) even tho i did everything exactly the same.
Im Using:
Node.js
NPM
Windows CMD Command Prompt
Sublime Text 3
Importing Packages
var Twit = require('twit');
var Keys = require('./private_auth_keys');
var T = new Twit(Keys);
Stream Setup.
I belive the bug is somewhere in the stream part but i dont get it... i did everything the same as the video. Maybe twitter blocked this from their API? idk what im talking about, but any feedback would be awesome.
var stream = T.stream('user');
stream.on('follow', followed);
function followed(eventMsg) {
console.log("New Follower Reply Sent!");
var Name = eventMsg.source.name;
var screenName = eventMsg.source.screen_name;
tweetIt('Heyyy .#' + screenName + ' thanks for the follow! Do you like memes? #RateThatMeme');
}
Reply/Tweet Function
function tweetIt(txt) {
var tweet = {
status: txt
}
T.post('statuses/update', tweet, tweeted);
function tweeted(err, data, response) {
if (err) {
console.log("oof! Something went wrong!");
} else {
console.log("Tweet sent successfully!");
}
}
}
Error Message
events.js:167
throw er; // Unhandled 'error' event
^
Error: Bad Twitter streaming request: 401
at Object.exports.makeTwitError (C:\Users\admin\Desktop\Projects Code Train\node\node2\node_modules\twit\lib\helpers.js:74:13)....etc
Twitter user streams were retired in August so this code will no longer work as is.
401 status code means you are not unauthenticated - there should be additional WWW-Authenticate header in response that will tell you more.
I think twitter API changed since this tutorial was recorded and you have to do a bit more now to be able to access it, this is probably a reason for 401 status. as far as I can see, the author moved to mastodon with this tutorial lately because of that.

Twitter API Javascript extended mode

I am using twitter stream API with Javascript in order to collect tweets. The problem is that I am not using the extended mode and I don't get the full tweet when it is greater than 140 characters. I have searched info and I discovered that I need to pass tweet_mode=extended in the request and the response will contain a full_text which is the tweet complete. The problem is that I don't know where to write tweet_mode=extended. This is my code:
twitter.stream('statuses/filter', {track: word, function (stream) {
stream.on('data', function (tweet) {
console.log(tweet.text)
});
stream.on('error', function (error) {
console.log("error:", error);
})
})
Unfortunately the streaming API does not have the option to add the tweet_mode param
Documentation here: https://developer.twitter.com/en/docs/tweets/tweet-updates
This paragraph is of note to your concern:
The Streaming API does not provide the same ability to provide query
parameters to configure request options. Therefore, the Streaming API
renders all Tweets in compatibility mode at this time.
...
Streaming API consumers should update their code to first check for
the presence of the extended_tweet dictionary, and use that in
preference to the truncated data as is applicable for their use case.
When extended_tweet is not present, they must fall back to using the
existing fields.
As Van said, tweets in streaming API are mixed. So you can try this :
twitter.stream('statuses/filter', {track: word, function (stream) {
stream.on('data', function (tweet) {
let text = tweet.extended_tweet?tweet.extended_tweet.full_text:tweet.full_text?tweet.full_text:tweet.text;
console.log(text)
});
stream.on('error', function (error) {
console.log("error:", error);
})
})

PouchDB and React-Native not replicating .to() but .from() is working

For some reason documents created on my app are not showing up on my remote couchdb database.
I am using the following
import PouchDB from 'pouchdb-react-native'
let company_id = await AsyncStorage.getItem('company_id');
let device_db = new PouchDB(company_id, {auto_compaction: true});
let remote_db = new PouchDB('https://'+API_KEY+'#'+SERVER+'/'+company_id, {ajax: {timeout: 180000}});
device_db.replicate.to(remote_db).then((resp) => {
console.log(JSON.stringify(resp));
console.log("Device to Remote Server - Success");
return resp;
}, (error) => {
console.log("Device to Remote Server - Error");
return false;
});
I get a successful response the response:
{
"ok":true,
"start_time":"2018-05-17T15:19:05.179Z",
"docs_read":0,
"docs_written":0,
"doc_write_failures":0,
"errors":[
],
"last_seq":355,
"status":"complete",
"end_time":"2018-05-17T15:19:05.555Z"
}
When I go to my remote database, document_id's that am able to search and grab on the application do not show up.
Is there something I am not taking into account?
Is there anything I can do to check why this might be happening?
This worked when I used the same scripting method in Ionic and when I switched to React-Native I noticed this is the case.
NOTE: When I do .from() and get data from remote to the device, I get the data. For some reason it just isn't pushing data out
"Is there anything I can do to check why this might be happening?"
I would try switching on debugging as outlined here.
PouchDB.debug.enable('*');
This should allow you to view debug messages in your browser's JavaScript console.

Categories

Resources