Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
132
rated 0 times [  137] [ 5]  / answers: 1 / hits: 10701  / 4 Years ago, fri, february 21, 2020, 12:00:00

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!


More From » node.js

 Answers
10

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.


[#4683] Tuesday, February 18, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
josefn questions
;