ReferenceError: io is not defined - socket.io - node.js

I'm trying to connect my socket.io server from the Electron app but I get this error:
How can I fix this problem? I've tried every single types of connecting but i cant get io() so I can't connect the server.
Index.html:
Script to connect to server:

I found the problem here!
I added payload.js manually in index.js, not in with webPreferences attribute in main.js and I set the type as module like this: <script type="module" src="payload.js"></script>
and now my electron app is connecting the server perfectly!

Related

link sails.io.js in angular2

I'm having trouble using sails.io.js with angular2. I use the angular2-sails library.
First I decided to use with a code inside my app.component.ts
this._sailsService.connect(this.url);
It works but when I launched the app I also have those messages "Socket is trying to reconnect to Sails.." I checked, it's trying to connect to port 4200.
I used sails.io.js with
"scripts": ["sails.io.js"]
in angular-cli.json. But I tried something else :
<script src="sails.io.js"> // I also tried src="src/sails.io.js
</script>
<script type="text/javascript">
io.sails.useCORSRouteToGetCookie = false;
io.sails.url = 'http://localhost:1337';
</script>
in index.html, and my browser returns that it cannot load the script at "http://localhost:4200/sails.io.js" or "http://localhost:4200/src/sails.io.js"
folders :
app.js
angular-cli.json
src
-index.html
-sails.io.js
-app
--app.component.ts
I don't know why it tries to connect to 4200 when it's called in the angular-cli.json and why it can't access my sails.io.js when called in the index.

socket.io including in project and installation

Im want to include socket.io in my project. I installed it using npm install -g socket.io still being in directory my files to the project are located in.
I created script to test if it was successfull
i creater server.js with code
var client=require("socket.io").listen(8080).sockets;
client.on("connection",function(socket){
console.log("Someone has connected")
})
and included in html file
<script type="text/javascript" src="http://127.0.0.1:8080/socket.io/socket.io.js"></script>
I tried to run node server.js to see if there will be message in console but there wasnt , . Whad did i do wrong? I a trying to solve this problem but can find solution. Thanks for helping.
You must initialize a connection in html file (on client side).
If you are trying to connect to the host that serves the page you can use:
var socket = io();
or specify address:
var socket = io.connect('http://127.0.0.1:8080');

socket.io on openshift with angular-fullstack

I generated a node.js app using Yeomans angular-fullstack generator.
Everything worked fine except the use of socket.io. After uploading my app to openshift using angular-fullstack:openshift, I got informed that I have to:
Openshift websockets use port 8000, you will need to update the client
to connect to the correct port for sockets to work.
in /client/app/components/socket/socket.service: var ioSocket = io.connect('http://my-domain.rhcloud.com/:8000')"
I dont know where to do this. I am using socket.io version 1.0.6 which is shown inside package.json file.
==> app-root/logs/nodejs.log <==
/var/lib/openshift/xxx/app-root/runtime/repo/server/config/socketio.js:41
socket.address = socket.handshake.address.address + ':' +
^
TypeError: Cannot read property 'address' of null
at Namespace. (/var/lib/openshift/xxx/app-root/runtime/repo/server/config/socketio.js:41:46)
at Namespace.EventEmitter.emit (events.js:95:17)
at Namespace.emit (/var/lib/openshift/xxx/app-root/runtime/repo/node_modules/socket.io/lib/namespace.js:205:10)
at /var/lib/openshift/xxx/app-root/runtime/repo/node_modules/socket.io/lib/namespace.js:172:14
at process._tickCallback (node.js:415:13)
DEBUG: Program node server/app.js exited with code 8
DEBUG: Starting child process with 'node server/app.js'
By the way, the app, including socket.io, works fine on my local development machine!
Thanks for any help!
Fall.Guy
My working solution is now:
I changed the creation of the openshift app to "not create a scalable app" (I disabled the -s switch in the yo angular-fullstack:openshift command)
After that, I changed the connection of socket.io to:
var ioSocket = io('http://my-domain.rhcloud.com:8000', {
// Send auth token on connection, you will need to DI the Auth service above
// 'query': 'token=' + Auth.getToken()
});
in the "/client/components/socket/socket.service.js" file.
Socket.io works now fine!
I'm not sure why openshift requires this, but you just need to modify your client code.
In your client side code, you should have something like this:
var socket = io();
You'll apparently need to update it to:
var socket = io('http://mywebsite.com:8000');
It's worth noting that there are plenty of other hosting providers (even free) which don't require you to jump through these hoops.

Socket.io Express Application I can't import a socket.io library in my Jade Client

I have a Node Express application and i want to implement a socket.io, but when I want import the socket.io library the javascript console shows to me:
Failed to load resource: the server responded with a status of 404 (Not Found)
http: //localhost:3000/socket.io/socket.io.js
My Jade code is as follows
extends layout
block content
script(src="/javascripts/jquery-2.0.3.min.js")
script(src="/socket.io/socket.io.js")
//var socket = require('./sockets');
//var socket = io.connect();
form(name='room', action='', method='post')
input(id='roomName', type='text', name='roomName')
input(id='button', type='button', value='crear sala' onclick='addRoom()')
The things that I've done already:
->I've installed the socket package via npm correctly.
->I've tried to load the socket.io.js from my browser but the full route isn't created in my app.js
Regards
In order to access socket.io.js, you must have a Socket.IO server running.

Socket.io.js not found on Ubuntu but is on Windows

I've been pulling out my hair trying to figure this problem out. I have a node.js app that works fine in windows. I zipped everything up and put it on my linux box (Ubuntu 12) and installed all of the libraries through npm, and yet I still get a 404 message saying my socket.io.js file cannot be found. I've tried various solutions such as linked to the cdn.socket.io script but that just throws a "require not found" error. My code in my html is as follows:
<script src="/socket.io/socket.io.js"></script>
I've even tried <script src="localhost:4000/socket.io/socket.io.js"></script>
and here is my server side:
var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app);
...
app.listen(4000);
console.log('server started');
This question is similar, however the answer for it is simply is an updated express semantic which shouldn't apply to my code: socket.io.js not found
If my understanding is correct, the script path should work because when socket.io is running, it should direct that request to the right route. I don't really know what else I should look into for a fix, could it be something with the path in Ubuntu?
Any help would be very much appreciated!
Just tested this:
var io = require('socket.io').listen(8000);
Went to localhost:8000 and the 'welcome to socket.io' message showed up so I know it is running....
paths
node_modules: /home/alex/node_modules/socket.io
my node app: /home/alex/documents/project/app.js
I had the same problem and it seemed that the reason was that something went wrong during the installation of socket.io.
Actually the only workaround I found was to make a new directory, copy all your code there and do a fresh install of the Express and socket.io modules:
npm install express socket.io
Maybe I was lucky, but it worked!

Resources