How do I implement Socket.IO in an electron app? - javascript

I want to implement Socket.IO in an Electron app, however I have found no documentation and no examples of how this could work.
If someone could explain to me how two or more clients could communicate via the electron app, I would be very grateful!

You know, the electron app will be running at end user.
So you should create Socket server at somewhere sth like Cloud server and your electron app should contain one socket.io client instance.
At Socket server
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
And at frontend (your case Electron app side)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
</script>
or
// with ES6 import
import io from 'socket.io-client';
const socket = io('http://localhost');
So that users can communicate inside your Electron app.

Related

Can't connect to socket.io server from Node JS script

I've got a socket.io server running on Node JS listening for messages. I now need to start another Node JS script (my client) which will send messages to the server, that's at a specific URL and port.
I'm passing my URL into the connect function but am getting the error:
TypeError: io.connect is not a function
How can I pass my URL to connect to into this. My script is:
const io = require('socket.io');
const socket = io.connect('http://localhost:3000');
socket.on('connect', function () {
socket.send('hello') // send to server from running JS script
});
If you're attempting to initiate a socket.io connection from node.js, you need the client-side library which would be:
const io = require('socket.io-client')
And, of course, you have to install that library.

Connect to Heroku socket from another server/localhost

I have a nodejs/express app deployed on heroku with socket.io listening on port 80.
I want to connect to the app's socket from localhost which will be pushed to github-pages. I'm getting net::ERR_SSL_PROTOCOL_ERROR error in the GET request.
This is my server code deployed on heroku.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
io.on('connection', function(){
});
My localhost is running on webpack-dev-server.
import io from 'socket.io-client';
const socket = io.connect('https://tank3d.herokuapp.com:80');
Everything is fine if I run the server locally.

Connecting 2 servers via Node Websockets

I have 2 Raspberry Pi running on the same Network.
I am using one as a local web server for my house, I then have another one connected to some devices. I want them to both be able to communicate to each other via web sockets but am having some problems.
My server looks like this:
express = require('express'); //web server
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server); //web socket server
server.listen(8080); //start the webserver on port 8080
app.use(express.static('public')); //tell the server that ./public/ contains the static webpages
io.sockets.on('connection', function (socket) { //gets called whenever a client connects
socket.on('Testing',function(data){
console.log("Testing connection");
});
});
My problem comes with the client connection I am really not sure what code to use to try and connect.
I have installed Express and Socket.io on my client and used this code:
console.log('1');
// Connect to server
var io = require('socket.io')
var socket = io.connect('http://192.168.1.138:8080', {reconnect: true});
console.log('2');
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
console.log('3');
But this leads to an error on the io.connect is not a function.
I am not really sure how to get the client to work so any advice is appreciated.
I should add that connecting to my webserver directly via the ip and port does load the webpages I have created successfully.
When using socket.io on the server side as a client you need to do var socket = require('socket.io-client')('http://192.168.1.138:8080', ...);. See https://www.npmjs.com/package/socket.io-client

Issues with using Socket.io

I am trying to use socket.io for the first time, Im using it with a MEAN Stack. I have set it up the following way.
Server.js
var express = require('express');
var app = express(); // create our app w/ express
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.listen(port);
io.on('connection', function(socket) {
socket.emit('message', {
'message': 'hello world'
});
});
HTML File
<script>
var socket = io.connect();
socket.on('message', function(data) {
console.log(data.message);
});
</script>
I couldn't find the socket.io.js file so I searched for a CDN for the script. I used the following.
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js"></script>
I tried downloading the socket.io.js from their website but the website seems to be down. I can access it, I get the 502 BAD GATEWAY error (http://socket.io)
When I load my HTML page after setting everything up as shown above I get the following error in the console.
Any help will be greatly appreciated!
UPDATE:
The socket.io server will perform some magic to provide that file when you retrieve it like this:
<script src="/socket.io/socket.io.js"></script>
EDIT: ah, I see the problem: your Express setup is incorrect.
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.listen(port);
You're attaching socket.io to a server created by http.createServer, but with app.listen() you're actually creating a new server, one that socket.io isn't attached to.
You can make the setup a bit simpler:
var express = require('express');
var app = express();
var server = app.listen(port);
var io = require('socket.io')(server);
No need to use http.
As of now, socket.io website is still down. If you don't have the file in your server, it does not help using /socket.io/ because this points to a route on your server and that route doesn't seem to exist on your app. With this config, you need to be serving the .js file statically from your server - but I don't see that on your server code.
When you try the CDN, make sure the file is being loaded.

What's the best way to implement socket.io as a submodule within ExpressJS?

I'm trying to build an ExpressJs based app using the submodule architecture suggested by tjholowaychuk.
I'd like to also have some realtime socket interaction, so I'm trying to integrate socket.io. I'm struggling to find the best way to do this as a sub app and allow both Express and socket.io listen together.
You could put all socket.io related code in a separate file:
// socketio.js
var sio = require('socket.io');
module.exports = function(server) {
var io = sio.listen(server);
io.sockets.on('connection', ...);
return io;
};
// app.js
var app = require('express')(),
server = require('http').createServer(app),
io = require('./socketio')(server);

Categories