I only need socket.io
to emit
messages to clients
, if a new object is inserted to database. So my idea was to emit the message directly from my controller’s insert-method
. In my server.js
file, i'm creating the socket.io object
and try to make it accessible for other modules:
var server = require('http').createServer(app); var io = require('socket.io').listen(server); //make socket io accessible for other modules module.exports.io = io;
In my controller I have tried using socket.io in this way:
var io = require('../server').io; ... io.sockets.on("connection", function(socket){ passportSocketIo.filterSocketsByUser(io, function (user) { return user.workingAt === socket.handshake.user.workingAt; }).forEach(function(s){ s.send("news", insertedObject); }); });
And here I'm stuck. The "connection" event will never be fired and so the message will not be emitted. Is that the correct way to use socket.io
in separate
files? Unfortunately I cant find complex socket.io
example.
Socket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and server. It has two parts: a client-side library that runs in the browser, and a server-side library for node.
Socket.IO Client Library for Unity (mono / . NET 2.0), which is ported from the JavaScript client version 1.1. 0. SocketIoClientDotNet by Quobject is a very good project, but it does not support Unity.
Pusher is the category leader in delightful APIs for app developers building communication and collaboration features. On the other hand, Socket.IO is detailed as "Realtime application framework (Node. JS server)". Socket.IO enables real-time bidirectional event-based communication.
You are trying to invert the flow of control. The way to do it is for your controller to implement an interface (an API) that your server can use to pass control to.
A simple example would be:
In mycontroller.js
// no require needed here, at least, I don't think so // Controller agrees to implement the function called "respond" module.exports.respond = function(socket_io){ // this function expects a socket_io connection as argument // now we can do whatever we want: socket_io.on('news',function(newsreel){ // as is proper, protocol logic like // this belongs in a controller: socket.broadcast.emit(newsreel); }); }
Now in server.js
:
var io = require('socket.io').listen(80); var controller = require('./mycontroller'); io.sockets.on('connection', controller.respond );
This example is simple because the controller API looks exactly like a socket.io callback. But what if you want to pass other parameters to the controller? Like the io
object itself or the variables representing end points? For that you'd need a little more work but it's not much. It's basically the same trick we often use to break out of or create closures: function generators:
In mycontroller.js
module.exports.respond = function(endpoint,socket){ // this function now expects an endpoint as argument socket.on('news',function(newsreel){ // as is proper, protocol logic like // this belongs in a controller: endpoint.emit(newsreel); // broadcast news to everyone subscribing // to our endpoint/namespace }); }
Now on the server we'd need a bit more work in order to pass the end point:
var io = require('socket.io').listen(80); var controller = require('./mycontroller'); var chat = io .of('/chat') .on('connection', function (socket) { controller.respond(chat,socket); });
Notice that we pass socket
straight through but we capture chat
via a closure. With this you can have multiple endpoints each with their own controllers:
var io = require('socket.io').listen(80); var news_controller = require('./controllers/news'); var chat_controller = require('./controllers/chat'); var news = io .of('/news') .on('connection', function (socket) { news_controller.respond(news,socket); }); var chat = io .of('/chat') .on('connection', function (socket) { chat_controller.respond(chat,socket); });
Actually, you can even use multiple controllers for each endpoint. Remember, the controllers don't do anything apart from subscribing to events. It's the server that's doing the listening:
var io = require('socket.io').listen(80); var news_controller = require('./controllers/news'); var chat_controller = require('./controllers/chat'); var chat = io .of('/chat') .on('connection', function (socket) { news_controller.respond(chat,socket); chat_controller.respond(chat,socket); });
It even works with plain socket.io (no endpoints/namespaces):
var io = require('socket.io').listen(80); var news_controller = require('./controllers/news'); var chat_controller = require('./controllers/chat'); io.sockets.on('connection', function (socket) { news_controller.respond(socket); chat_controller.respond(socket); });
You can do this very easily you have to only write socket connection in app.js and than you can use socket anywhere you want
In app.js
file put code like below
var http = require('http').createServer(app); const io = require('socket.io')(http); io.sockets.on("connection", function (socket) { // Everytime a client logs in, display a connected message console.log("Server-Client Connected!"); socket.join("_room" + socket.handshake.query.room_id); socket.on('connected', function (data) { }); }); const socketIoObject = io; module.exports.ioObject = socketIoObject;
In any file or in controller you can import that object like below
const socket = require('../app'); //import socket from app.js //you can emit or on the events as shown socket.ioObject.sockets.in("_room" + req.body.id).emit("msg", "How are You ?");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With