access io.sockets from app.js when using the MVC framework

472 views
Skip to first unread message

antonio.c...@gmail.com

unread,
Feb 20, 2013, 6:04:25 PM2/20/13
to node...@googlegroups.com
Hi,

I am trying to access io.sockets to bind a callback to the "connection" event, but I am not able to access the "io" object from app.js or websocket/*.js anyway.
I am using the MVC Node.ACS framework and while I can access app and express objects from the start function defined in the app.js, I don't know how to access io.

Could you provide me with some directions?

best
Antonio

Peter Lee

unread,
Feb 20, 2013, 6:23:40 PM2/20/13
to node...@googlegroups.com, antonio.c...@gmail.com
If you are trying to use built-in web socket function, please refer http://nodeacs.cloud.appcelerator.com/guides/quickstart : The Chat Room App.

antonio.c...@gmail.com

unread,
Feb 21, 2013, 3:18:19 AM2/21/13
to node...@googlegroups.com, antonio.c...@gmail.com
Hi Peter,

of course I have read all the docs you have pointed at, but I cannot catch the "connection" event.

I added a new event in the config.json:

"websockets":
[
{ "event": "vote", "callback": "poll#pollChoice"},
{ "event": "connection", "callback": "poll#connection"}
]

and defined a callback "connection" in websockets/poll.js:

function connection(data, socket) {
console.log("connected")
console.log(data);

}

but it is never called.

Generally, you should listen this event on:

io.sockets.on('connection', function (socket) {
// do something
});

but I cannot get a reference to the "io" object in any file.
In the doc, you say that before you call the start() function, you initialize both app/express and io, providing hooks to add further options to both app and express object. Could you expose io in the same way, like:

function start(app, express, io) ?

best
Antonio

antonio.c...@gmail.com

unread,
Feb 21, 2013, 9:34:58 AM2/21/13
to node...@googlegroups.com, antonio.c...@gmail.com
Moreover,

being not able to able to access io.sockets, we can't count for example the list of connected clients with:

var clients = io.sockets.clients()

How can I manage that?

thank u
Antonio

Peter Lee

unread,
Feb 21, 2013, 2:59:15 PM2/21/13
to node...@googlegroups.com, antonio.c...@gmail.com
On MVC if you have defined websocket route in config.json, node.acs will automatically run io.sockets which you don't have to call connection event.

// config.json
'websockets': [{"event": "a", "callback": "ctr#a"}, {"event": "b", "callback": "ctr#b"}]

// node.acs will handle as
io.sockets.on('connection', function (socket) {
  // loop through websocket route config.json 
  socket.on('a', <<function ctr#a>>);
  socket.on('b', <<function ctr#b>>);
});

if you want to get clients info (tokens)

socket.on('b', function(){
  // if you are not using rooms
  console.log( socket.manager.rooms[''] );
  // If there's a room such socket.join('room'), you need to add slash socket.manager.rooms['/room']
  console.log( socket.manager.rooms['/room'] );
});

Will Dent

unread,
Mar 13, 2013, 1:09:45 PM3/13/13
to node...@googlegroups.com, antonio.c...@gmail.com
Peter,
What would you do if your not using your MVC but just using without framework?

Peter Lee

unread,
Mar 13, 2013, 1:58:24 PM3/13/13
to node...@googlegroups.com, antonio.c...@gmail.com
I think you can just implement socket.io(http://socket.io/) yourself.

lpjand...@gmail.com

unread,
Jul 29, 2013, 11:42:32 PM7/29/13
to node...@googlegroups.com, antonio.c...@gmail.com
Hi,

Did you ever get any information on how to get access to the io object from app.js?

I am looking for info relating to this - I would like to send out data on the socket from one of my normal controllers (i.e. not the websocket controllers) but I cannot find how to get access to the io object to do this send.

If anyone knows I would appreciate some pointers please.

Cheers,
lars

toby....@gmail.com

unread,
Oct 16, 2013, 11:10:30 AM10/16/13
to node...@googlegroups.com, antonio.c...@gmail.com

Hey dude. I'm trying to do exactly the same thing, a few months later.
Did you ever find a solution?

Yuping Jin

unread,
Oct 17, 2013, 5:19:30 AM10/17/13
to node...@googlegroups.com, antonio.c...@gmail.com
As Peter replied before, the MVC framework includes a thin wrapper which is already listening on 'connection'. The framework registers events and their handlers based on config.json. Can you please provide more detail about what you cannot do with this pattern and is there any workaround?

Thanks and regards,
Yuping

Yuping Jin

unread,
Oct 17, 2013, 8:56:12 PM10/17/13
to node...@googlegroups.com, antonio.c...@gmail.com, toby....@gmail.com
We're working on a solution for this.

Thanks and regards,
Yuping

Yuping Jin

unread,
Oct 17, 2013, 11:01:59 PM10/17/13
to node...@googlegroups.com
For now you can also try socket.manager to get the io object. Just remember that the framework is already listening on '/'.


--
You received this message because you are subscribed to a topic in the Google Groups "Node.ACS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-acs/qFKR9tY3GH4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-acs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
靳宇平(Jin Yuping)
jiny...@gmail.com

daniel...@gmail.com

unread,
Jan 26, 2015, 2:27:36 PM1/26/15
to node...@googlegroups.com
Hi, Did this ever get resolved? I am trying to do exactly this.
Thanks,
Dan D
Reply all
Reply to author
Forward
0 new messages