I inherited a project which utilizes node.js and socket.io, both of which I am not too familiar with.
I installed node.js and npm via homebrew and socket.io via npm.
The current setup has two different "sites": The frontend, that the user interacts with, and the backend that handles all the communications with a DB. The application uses several different displays, and the previous developer deemed it best to use the current setup.
I can successfully start the node server by invoking node node-server.js. In the head of the node-server.js, socket.io is being called like:
var sys = require('sys')
var exec = require('child_process').exec;
var io = require('socket.io').listen(8090);
[...]
In my front end, I call socket.io like this:
<script src="http://192.168.1.111:8090/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.111:8090');
[...]
However, when I debug the page, I get the following error messages:
SyntaxError: Unexpected identifier 'to' (anonymous function) socket.io.js:1
ReferenceError: Can't find variable: io sample.html
In the terminal window, where I started the node server, I get the output:
debug - served static content /socket.io.js
I should add that the two sites reside in separate folders - I access the frontend through a local Apache server, the piece that interacts with the DB sits someplace else on the same machine.
How would I correctly call the socket.io in my client?
Is there a way to enable better debugging?
Update
So the to in the error message, is because whenever the browser tries to load the socket.js file, it gets the reply of Welcome to socket.io
Related
I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE
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.
In my electron app , I load the remote page(index.html) from remote url like("http://xxxx/index.html"), then I am trying to send a ipc event to the main process then handle it , and failed, but if I put the the index.html in local fs and it is OK.
So my question is how to enable the page to access the node api (like require,ipc, etc.) from the remote page.
------ (main process)
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
mainWindow.loadUrl('http://localhost:8080');
//mainWindow.loadUrl('file://' + __dirname + '/index.html');
var ipc = require('ipc');
ipc.on('spawn-ext-process', function () {
console.log("spawn-ext-process");
});
--------- http://localhost:8080/index.html (render process)
<script>
var ipc = require('ipc');
ipc.send('spawn-ext-process');
</script>
AFIAK the ipc api doesn't use http. It essentially is sending a signal from one part of the program running on your local machine to another part of the same program on that machine.
If you want to send a signal over http you might want to use something like socket.io or websockets.
How to enable the page to access the node api (like require,ipc, etc.) from the remote page?
Depends on how you're loading the page. If you're using loadUrl on a BrowserWindow instance, node integration is enabled by default (you can disable it via the nodeIntegration option, see the docs).
If you're using a <webview> tag, then it's disabled by default and you can enable it via the nodeintegration attribute. See the docs. Note that with webview you can preload scripts that do have access to node and electron APIs safely, and then access to those objects are destroyed when the script has finished executing (see here).
Like #FelipeBrahm said, I would not give a remote page access to node.js or electron APIs unless it's your own page, and even if you do that, be cautious.
this is my first node.js and socket.io application , i didn't use express ,I want to deploy the application on heroku do i need to use it ? i mean i just did npm install socket.io on localhost and in my server file i.e game.js i have io = require("socket.io") and socket = io.listen(Number(process.env.PORT)) only and in one of the files where from where i am sending the message i have socket = io.connect();
so please tell me if i need to use express and how show i modify my existing application ?
I have given the link to the source of application
( https://github.com/bitgeeky/herokutest )
Although the Application works fine on localhost by changing the port no , to some port no like (8000) but Heroku error log on doing "heroku open" is http://pastebin.com/MtB0z5vQ
I noticed that you haven't created a http server. I am assuming that you are creating a web application, since you are deploying to heroku. For that, you need to create a http server in nodejs.
Go through socket.io https://github.com/LearnBoost/socket.io
Also http://socket.io/#how-to-use
This should get you started
Note: You do not need express. But it will make your work easier in many ways. Depends on the type of application that you want to create.
I am using this on the client
<script type="text/javascript" src="http://domain.com:8440/socket.io/socket.io.js"></script>
When the server is too busy, sometimes it gives an "io not defined" error on the client. How can I resolve this ?
Here is my server code
var db_helper = require("./db.js");
var io=require('socket.io').listen(8440);
var check = require('validator').check,
sanitize = require('validator').sanitize;
var roomid=0;
var anonid;
var ilet;
var userip;
var blck_id;
io.set('transports', [
, 'xhr-polling'
, 'websocket'
, 'jsonp-polling'
]);
The server has 16 GB RAM and 13.6 GHz CPU.
Keep in mind node.js apps run in a single thread unless you're using something like Cluster to run more. This means that if you're doing something that blocks, it's possible for the requests to http://domain.com:8440/socket.io/socket.io.js to timeout, which would cause your io not defined error. You should see a 404 error in your console logs as well if / when this happens.
Try this out,
get the clientside socketio library and put it in the folder from where the js files are served. Point the script location to this file location.
You will find the client side script here
<server node_moudules>\socket.io\node_modules\socket.io-client\dist\socket.io.js
In the first place, as pointed by #Timothy try to find out why the node is getting busy.
You can try to serve the socket.io.js file from a regular web server, as Chandu pointed out.
This should at least offload the node.js server from that load.
Secondly - and this is more important - look at what is blocking your node.js server. As node.js is single threaded, you should by all means avoid long-running operations.
Can you give an example of the code you are running in node?