Impossible to get socket.io from a Express route

1,644 views
Skip to first unread message

The Coder

unread,
Oct 11, 2011, 10:42:49 AM10/11/11
to Socket.IO
I will be crazy! I have a simple form that i send data via
form(method="post",action="/new")

In the the server, NodeJS with ExpressJS and Jade, i have a simple
app.post. How can i do somethink like it:

app.post('/new', function(req, res) {
socket.emit('test', {text: 'Hi! I will fuck your mind!'});
});

Please, help-me!
Very very thanks!

PyroStrex

unread,
Oct 11, 2011, 11:20:09 AM10/11/11
to Socket.IO
I think you can't do something like that. But if you are going to post
using AJAX, then you can.

Bruce Doan

unread,
Oct 11, 2011, 11:36:14 AM10/11/11
to sock...@googlegroups.com
AFAIK, it's impossible to do that

Daniel Shaw

unread,
Oct 11, 2011, 1:39:43 PM10/11/11
to sock...@googlegroups.com
Actually not that crazy... and this seems to be a very popular,
emerging idiom with Socket.io v0.7+.

Get a room.

// in your socket.io code
sio.on('connection', function (socket) {
socket.join('vroom');
});

// ...elsewhere, in your app
app.get('/', function (req, res) {
io.sockets.in('vroom').emit('accelerate', { text: 'Go faster!'});
});

That's it.

I'll be releasing a module later today which provides an even easier,
more expressive way of doing this for large scale deployments of
Socket.io.

Daniel Shaw
@dshaw

The Coder

unread,
Oct 12, 2011, 12:17:24 AM10/12/11
to Socket.IO
Wow! 2 Impossibles and one solution! I will try and inform!

Daniel, that Jesus bless you, thanks!!!

Tom

unread,
Oct 12, 2011, 6:00:39 AM10/12/11
to sock...@googlegroups.com
That vroom group example is rather meaningless, as it lacks a way to couple a given requesting client to the corresponding group.

What you need to do, is pass in some kind of identifier while making the request which links the request to one of the websocket clients.

Tom

2011/10/12 The Coder <thal...@gmail.com>

Mike Kunze

unread,
Oct 12, 2011, 7:36:20 AM10/12/11
to sock...@googlegroups.com
What Tom said..


While the server receives a connect emit from the client, send the socket.id as an emit from the server.  Save it client side and pass it when you POST, as a hidden variable perhaps.


Back on the server:
io.sockets.socket(socket.id).emit('formPost', { success: true, msg: 'You win!'} );

Sending and emitting messages to a particular socket

io.sockets.socket(< session id>).send('my message')
io.sockets.socket(< session id>).emit('event name'[, arguments])



Mike

The Coder

unread,
Oct 12, 2011, 10:12:23 AM10/12/11
to Socket.IO
Well @Tom, what i need is not to know, but how to do :) I'm stucked in
it and Mr. Google don't want to help me :)

Thanks!

PS: @Daniel, I have not implemented your code, today is Children's Day
here! Yeah! :)

On 12 out, 07:00, Tom <tommed...@gmail.com> wrote:
> That vroom group example is rather meaningless, as it lacks a way to couple
> a given requesting client to the corresponding group.
>
> What you need to do, is pass in some kind of identifier while making the
> request which links the request to one of the websocket clients.
>
> Tom
>
> 2011/10/12 The Coder <thales...@gmail.com>

The Coder

unread,
Oct 12, 2011, 11:12:09 AM10/12/11
to Socket.IO
I read it, but could not implement :( Sorry. Please, can someone show
a full code (server and client side) with it in use, tested!? Here, my
group stucked in it!

Thanks ;)

On 12 out, 08:36, Mike Kunze <michael.ku...@gmail.com> wrote:
> What Tom said..
>
> While the server receives a connect emit from the client, send the
> socket.idas an emit from the server.  Save it client side and pass it
> when you POST,
> as a hidden variable perhaps.
>
> Back on the server:
> io.sockets.socket(socket.id).emit('formPost', { success: true, msg: 'You
> win!'} );
>
> https://github.com/LearnBoost/Socket.IO/wiki/Migrating-0.6-to-0.7+
> Sending and emitting messages to a particular socket
>
> io.sockets.socket(< session id>).send('my message')io.sockets.socket(<
> session id>).emit('event name'[, arguments])
>
> Mike
>
>
>
>
>
>
>
> On Wed, Oct 12, 2011 at 5:00 AM, Tom <tommed...@gmail.com> wrote:
> > That vroom group example is rather meaningless, as it lacks a way to couple
> > a given requesting client to the corresponding group.
>
> > What you need to do, is pass in some kind of identifier while making the
> > request which links the request to one of the websocket clients.
>
> > Tom
>
> > 2011/10/12 The Coder <thales...@gmail.com>

Daniel Shaw

unread,
Oct 12, 2011, 11:57:35 AM10/12/11
to sock...@googlegroups.com
There's a great blog post on the wiki that goes into greater depth
about using rooms crossing from Socket.io to Express/Connect.
Hopefully, that can get you started.
http://www.danielbaulig.de/socket-ioexpress/
http://www.danielbaulig.de/socket-ioexpress/#comment-1158

Daniel Shaw
@dshaw

Mike Kunze

unread,
Oct 12, 2011, 2:08:38 PM10/12/11
to sock...@googlegroups.com
Just to be sure, you are doing ajax for your POST, right?  If you are not, and doing it the ol fashioned way, your client's socket.id will change because you are initializing a new socket session when the browser reloads.

If you are indeed using ajax, here are the important bits:

// Client side javascript
var socket = new io.connect('http://domain.com');
    
socket.on('connection received', function(data) {
  myGlobalAppNameSpace.socket = data;
});

// send myGlobalAppNameSpace.socket.id as a hidden variable on ajax form submit.



// Server side javascript
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
  console.log('socket_id: ' + socket.id + ' has connected');
  
  socket.emit('connection received', {
    timestamp: Date.now(),
    socket_id: socket.id
  });
});

The Coder

unread,
Oct 12, 2011, 10:54:09 PM10/12/11
to Socket.IO
Thanks Mike!

On 12 out, 15:08, Mike Kunze <michael.ku...@gmail.com> wrote:
> Just to be sure, you are doing ajax for your POST, right?  If you are not,
> and doing it the ol fashioned way, your client's socket.id will change
> because you are initializing a new socket session when the browser reloads.
>
> If you are indeed using ajax, here are the important bits:
>
> // Client side javascript
>
> var socket = new io.connect('http://domain.com');
>
> socket.on('connection received', function(data) {
>   myGlobalAppNameSpace.socket = data;
>
> });
>
> // send myGlobalAppNameSpace.socket.id as a hidden variable on ajax form submit.
>
> // Server side javascript
>
> var io  = require('socket.io').listen(app);
>
> io.sockets.on('connection', function(socket) {
>   console.log('socket_id: '  + socket.id + ' has connected');
>
>   socket.emit('connection received', {
>     timestamp: Date.now(),
>     socket_id: socket.id
>   });
>
>
>
>
>
>
>
> });

Caesar Chi

unread,
Oct 13, 2011, 9:07:30 PM10/13/11
to sock...@googlegroups.com
like Mike said, you can asign socket and its socket.id in an public array. Then u can emit event to the socket.

PyroStrex

unread,
Oct 19, 2011, 7:01:08 PM10/19/11
to Socket.IO
No, it's not 2 impossibles, basically what I was saying was, you need
to use ajax to being able to do this. For the client-sided, JQuery has
the simplest way to achieve this. And yeah, Mike has proven me lol.
Thanks Mike.
Reply all
Reply to author
Forward
0 new messages