15

i have a project and I'm using socket.io with express ,

so what i need (i tried) is broadcasting a message but from an express action. is this possible i don't know how to get a reference to send or broadcast.

app.get('/', function(req, res) {
//i need to send messages from here 
});

Other things like using both express+socket.io is working with me :)

4 Answers 4

4

As long as I understand,

Why not use the socket message type as an event instead of a http get or post? On the client side you would send a message via the websocket with let's say an event property.

So in your case:

<script>
  // Initialize socket.io ...

  // and then
  socket.send({event: 'homepage loaded', foo: 'bar'});
</script>

And on the server side:

var io = io.listen(server);

io.on('connection', function (client) {
  client.on('message', function (message) {
    if (message.event == 'homepage loaded') {
      client.broadcast(...);
    }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

4

You might want to have a look at my socket.io + Express primer. What you want is covered in detail there.

// Send to all connected sockets
io.sockets.send('Hello, World!');

// Send to all sockets in a specified room
io.sockets.in('room').send('Hello, Room!');

Where io is the value returned by the call to socketio.listen(). You can place that code anywhere in your application, eg in your app.get callbacks.

Comments

0

Check out my example repo where I use ExpressJS + Juggernaut(pubsub over socket.io):

http://github.com/shripadk/express-juggernaut-demo

This might be overkill for what you need as it uses Publish/Subscribe. But it does, to a certain extent, solve your issue of using regular ExpressJS routes. Checkout the master branch after cloning the repository:

git checkout master

11 Comments

When i execute "git clone git://github.com/shripadk/express-juggernaut.git" i get this error:"fatal: The remote end hung up unexpectedly"
Do you mean executing this command "git clone git://github.com/shripadk/express-juggernaut-demo.git",this is work for me but when i make "git checkout master" i get "Already on 'master' ", i ignored this and run the demo put i get this
The 'sys' module is now called 'util'. It should have a similar interface. node.js:50 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Cannot find module 'utils' at resolveModuleFilename (node.js:265:13) at loadModule (node.js:231:20) at require (node.js:291:14) at Object.<anonymous> (/home/ibrahim/ws/Lebanon/staTest/express-juggernaut-demo/lib/support/juggernaut/lib/juggernaut/channel.js:2:13) at Module._compile (node.js:348:23) at Object..js (node.js:356:12) at Module.load (node.js:279:25)
at loadModule (node.js:251:12) at require (node.js:291:14) at Object.<anonymous> (/home/ibrahim/ws/Lebanon/staTest/express-juggernaut-demo/lib/support/juggernaut/lib/juggernaut/publish.js:4:15)
Use node v0.2.4! I guess you are on node v0.3.0! you need to wait till all modules work with v0.3.0 to use that version. :)
|
0

I Found a nice example how to make what i need but with faye it's here http://nodecasts.org/.

I don't know the difference between Juggernaut ,Faye and direct Socket.io but Faye is good

for my case .And i think both of them use Socket.io internally.

1 Comment

Faye doesn't use socket.io internally. It is based on bayeux protocol. However, it is good :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.