Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using socket.io from a module

My sio = require('socket.io').listen(app) is in my server.js file, but I'm calling a method in a library that would like to push a message to the client... say api.user.pushToClient()

How am I able to access sio.sockets from there? Perhaps my structure is incorrect?

Folder structure:

server.js

api

|--user.js

|--another.js

like image 550
Mike Avatar asked Apr 06 '12 10:04

Mike


People also ask

How do I connect to a Socket.IO server?

listen(port); // Create a Socket.IO instance, passing it our server var socket = io. listen(server); // Add a connect listener socket. on('connection', function(client){ console. log('Connection to client established'); // Success!

Can you use Socket.IO without node JS?

The short answer is yes.

Can we use Socket.IO in Python?

The socketio. Server() class creates a server compatible with the Python standard library.

Should I use Socket.IO or Websockets?

Both WebSocket vs Socket.io are popular choices in the market; let us discuss some of the major Difference Between WebSocket vs Socket.io: It provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback.


1 Answers

in server.js append this line

module.exports.sio = sio; 

in api/user.js

sio = require('../server').sio;
sio.sockets.on ...

Or did I misunderstand the question?

like image 98
janfabian Avatar answered Nov 15 '22 08:11

janfabian