Generate node-xmpp script for browser - node.js

I am developing an application on php. I need a chat on xmpp nodejs. Sending a message will go from web page.
I found enter link description here. In terminal everything works fine. But how do I attach client script to the browser?
I generate script by:browserify node_modules/node-xmpp/lib/node-xmpp-browserify.js > nodeXmpp.js and attach it to web page:
Then trying to use it:
$(document).ready(funcrion(){
var client = new XMPP.Client({
jid: 'user#example.com',
password: 'password'
});
});
And chrome console telling me:
Cannot load StringPrep-0.2.3 bindings (using fallback). You may need to npm install node-stringprep nodeXmpp.js:3669
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery.js:3254
Uncaught TypeError: Object # has no method 'resolveSrv'
Object # - its "dns".
And before generate script i install node-stringprep.
Question is how build xmpp client script for browser.

You didn't post your code, but the following should work fine:
require('node-xmpp/lib/node-xmpp-browserify.js');
var client = new XMPP.Client(opts);

Related

open webpage when start nodejs server [duplicate]

This question already has answers here:
How to use nodejs to open default browser and navigate to a specific URL
(8 answers)
Closed last month.
I was wondering if and how i can automatically open a webpage when i start my server with nodejs
app.listen(app.get("PORT"), () => {
console.log(`Servidor corriendo en http://localhost:${app.get("PORT")}`)
})
I have this basic example where when i run node start proyect it will start the server and then i manually need to click on the console logged message or open a browser and type bla bla bla
I want to raise that web for itself like when in React we run the command npm run start
I tried to google it but i dont know how to search for this because i dont get any usefull info.
Thank u very much
Use open to Open stuff like URLs, files, executables. Cross-platform.
const open = require('open');
// opens the url in the default browser
open('http://stackoverflow.com');
UPDATE
open simplifies cross platform functionality, in node you can have access to the platform by process.platform and write platform specific executions
for example here is the native version in mac without using any NPM module
const url = 'http://stackoverflow.com';
require('child_process').exec(`open ${url}`);
I don't have access to a windows system to write the code for windows

socket.io client doesn't set sid in requests

I'm trying to build a nodejs script to communicate with a socket.io server.
const io = require('socket.io-client');
const socket = io('http://192.168.144.249');
socket.on('connect', () => {
console.log('connected');
})
Using wireshark to follow traffic I can see the following:
So the browser sends me a sid, both in the response body and in the cookie.
Unfortunately, my following requests do not include this sid and I received a 400 Bad Request error:
When I try to build the same client from a browser windows I can see that this cookie is indeed set, both as cookie and as a query parameter:
I don't want to use a browser, I want to use a standalone node script. As far as I understood the parsing of the response and the inclusion of the session id should be done automatically by the socket.io-client. Am I wrong? If so, how can I intercept this event so that I can send the sid with following requests?
Am I supposed to first do a simple http request to the server, get the sid from there and then add it to the socket.io client when creating it using custom cookies or custom query parameters?
I can also see that the node standalone script is using engine.io version 4 (EIO=4 in the GET requests), while the browser is doing it with engine.io version 3, but the respose received seems to be exactly the same so I don't really think this is what is preventing my script from automatically completing the handshake with the server.
Well, I was wrong. It was indeed a protocol mismatch problem.
I was able to make my script working using socket.io-client#1.0.2. Install it with:
npm install socket.io-client#1.0.2

How to get nodejs on client-side on cordova?

I am making a app with cordova. I want the coinmarketcap stats in it. I am using nodejs and it is working fine when I execute it in my command line. However I want to use it in my cordova application. This is the link to the npm package https://www.npmjs.com/package/node-coinmarketcap
When I try to run it in a browser I get the error message require not defined. I believe that error comes because nodesj is a server-side and browser is a client side ? Am I correct ?
My code
var CoinMarketCap = require("node-coinmarketcap");
var coinmarketcap = new CoinMarketCap();
coinmarketcap.multi(coins => {
console.log(coins.getTop(10)); // Prints information about top 10 cryptocurrencies
});
I expect that I can run my code in my browser or cordova application.
You are right, Node.js is a JavaScript run-time environment that executes JavaScript code outside of a browser. You have to find some client-side JS code or execute your node code on your server and send result to your Cordova app.

how to setup twilio sms api in ionic angular?

#types/twilio
ran the following code and
installed twilio npm package
npm install twilio
In my provider i have imported by specifying
import * as twilio from 'twilio'
when i run by ionic serve --verbose
my console show an error that it cannot find module '../../package.json'
eventhough the library file has one, I will attach an image for reference
Twilio developer evangelist here.
The main Twilio Node.js module is not intended for use in a front end application so is unlikely to work well for you there. You are most likely getting that error as the Twilio module relies on some core Node modules that are unavailable in a browser environment.
It is not a good idea to try to use Twilio APIs from the client side as you expose your account credentials which would allow a malicious attacker to use your account for their own purposes.
Instead, we recommend you build a server that interacts with the Twilio API and that you make calls to from your own front end. If you are trying to use any of the Twilio client side SDKs, like Twilio Video, Twilio Chat or Twilio Sync, then you should install their respective modules twilio-video, twilio-chat and twilio-sync. You will still need a server side component that can generate access tokens for those services though.
Edit
In Node.js I just did a quick test for importing the Twilio module using TypeScript 2.6.2. This worked for me:
import { Twilio } from 'twilio';
const client = new Twilio("MY_ACCOUNT_SID", "MY_AUTH_TOKEN");
client.messages.each({ limit: 10 }, function(message) {
console.log(message.body);
});
Could you try this format for importing?
import { * as twilio } from 'twilio'
As philnash said, Twilio is not designed to run clientside. I tried to do the same thing you were doing. And you just have to have your Twilio run on a server and have your angular connect to that server to run it. Easiest way. Send a POST to your server via ajax and then have it run what you need.

Get windows client username in Nodejs

I am developing a little application in nodeJS and AngularJS. all my clients are windows machines, and they are all in the same domain.
I want to add some text label in the page like " Welcome ! USERNAME" .
how can get the Windows username of the client when he gets homepage?
I read about passport and node-sspi, but didnt understand how to implement that.
thank you :)
Stackoverflow user 'azium', explains in this response that
"AngularJS is a client side application and only has access to the
variables that are delcared (sic) in the browser's window object. The
Environment object is something that is only accessible on the server
by default."
However, according to Arhsim Tima you can try the following javascript,
For IE:
var objUserInfo = new ActiveXObject("WScript.network");
document.write(objUserInfo.ComputerName+"<br>");
document.write(objUserInfo.UserDomain+"<br>");
document.write(objUserInfo.UserName+"<br>");
var uname = objUserInfo.UserName;
alert(uname);
For Firefox:
function getUser() {
return Components.classes["#mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('USERNAME');
}

Resources