configure socket.io to wss instead of ws - javascript

I am working on a small application that is hosted via node.js and uses socket.io for quick communication. For now it was only in development so it was fine. However now i would like to switch from ws:// to wss:// but dont really know where to start. I tried to research online but there is not much on the topic.
index.js (server)
const path = require("path");
const express = require("express");
const app = express();
const cors = require("cors");
const config = require("./config");
const webRoute = require("./lib/routes/web");
const widgetsRoute = require("./lib/routes/widgets");
const errorsHandler = require("./lib/handlers/errors");
const corsHandler = require("./lib/handlers/cors");
app.use(cors(corsHandler));
app.use(express.static(path.join(__dirname, "/public")));
app.use("/", webRoute);
app.use("/widgets", widgetsRoute);
app.use(errorsHandler);
app.listen(4118, "0.0.0.0");
const io = require("socket.io").listen(
require("http")
.createServer()
.listen(4113, "0.0.0.0")
);
require("./lib/inits/socket")(io);
client:
this.socket = io("http://example.org:4113");
In the network tab i can see that socket.io is using ws:// in one of its calls.
How can i modify this so it uses wss instead?

Related

Error: connection.once is not a function (Javascript)

I am writing a code that allows me to connect to the MongoDB database that I had made. For some reason, I am getting errors connecting my page and I don't know why. I have already looked at TypeError: connection.once(...).catch is not a function for help but that page did not answer my question. I also copied the code that my professor made and I can't seem to get it working.
This is my code:
const express = require('express');
const app = express();
const connection = require('./db/connection.js');
const dotEnv = require('dotenv').config();
connection.once('open', ()=>{
const server = app.listen(process.env.PORT || 8080, ()=>{
console.log("Connected and listening");
});
});
app.use(express.static('public'));
app.use(express.urlencoded({extended:true}));
const Film = require('./models/film.js');
And this one is the connection.js file
const mongoose = require("mongoose");
let mongoDB = `name of database (this is correct, I used this to connect to a different project and it worked`;
module.exports = mongoose.connect(mongoDB);

Cannot connect node.js and socket.io to my front end

I am trying like this, but anyway see only cannot get /
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
app.use(express.static(path.join(__dirname+"/public")))
server.listen(5000);

Client unable to connect to server with socket.io

I'm having trouble with being able to connect to my node.js server from an external domain. It works fine when running it locally using the http web server through node however when connecting externally, it loads the socket.io.js file just fine but when trying to use the socket it removes the port from the URL and cannot connect.
Instead of doing this in the network requests:
http://external-domain.com:3000/socket.io/?EIO=3&transport=polling&t=M06GOUU
it does this:
http://external-domain.com/socket.io/?EIO=3&transport=polling&t=M06GOUU
I'm not sure how to make it not remove the port from the connection. How do I go about fixing this?
SERVER
const path = require('path');
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const publicPath = path.join(__dirname, '../public');
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
app.use(express.static(publicPath));
server.listen(3000, () => {
console.log(`Server is up on port 3000`);
});
CLIENT SCRIPT TAG
<script src="http://external-domain.com:3000/socket.io/socket.io.js"></script>
CLIENT JS ON A DIFFERENT DOMAIN
var socket = io();
socket.connect('http://external-domain.com:3000');
socket.on('connect', function () {
console.log('Connected to server.');
});
Change from this:
var socket = io();
socket.connect('http://external-domain.com:3000');
to just this:
var socket = io("http://external-domain.com:3000");
And, you don't use the socket.connect() as you will already have requested the connection with the io("http://external-domain.com:3000"); call.
Explanation
The code:
var socket = io();
uses the page URL to connect to a socket.io server at that origin. That is not what you want (apparently).
If you wanted to use the .connect() method, it would be like this:
var socket = io.connect("http://external-domain.com:3000");
Note: var socket = io(url) is simply a shortcut for var socket = io.connect(url).
socket.connect() does not accept a URL as a parameter so you simply weren't using that correctly. It's just a synonym for socket.open().
Use io.connect("url")
var socket = io.connect("http://external-domain.com:3000", { rejectUnauthorized: false });
// { rejectUnauthorized: false } is an optional parameter.
Hope this works for you.

In node.js do I need http when building a socket.io/express app?

I have just started using node.js and I can build a simple app that responds to requests and has some basic routing using the express framework.
I am looking to create something using socket.io but I am slightly confused over the use of the 'http' module. I understand what http is but I don't seem to need it for the following to work:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.htm');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
I can serve a html page over http without requiring the http module explicitly with something such as:
var http = require('http');
If I am using express do I have any use for the http module?
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
...
server.listen(1234);
However, app.listen() also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:
var express = require('express');
var app = express();
var socketio = require('socket.io');
// app.use/routes/etc...
var server = app.listen(3033);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
...
});
source
http://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen
no, you probably don't need it.
You can use something like:
var app = require('express').createServer();
var io = require('socket.io')(app);
//Your express and socket.io code goes here:

Socket.io + Express: Express declaration error

My requires:
//app.js Socket IO Test
var app = require('express').createServer(),
redis = require('socket.io/node_modules/redis'),
io = require('socket.io').listen(app);
My error:
Warning: express.createServer() is deprecated, express
applications no longer inherit from http.Server,
please use:
var express = require("express");
var app = express();
How do I modify my declarations to avoid this error? I understand this methodology is now deprecated for Express, just not sure what it needs to be changed to...
Thanks in advance!
Simply replace var app = require('express').createServer() with:
var express = require("express");
var app = express();

Categories

Resources