1

I have an express node.js server serving Socket.io. I would like the ability to make get requests to the express server that will automatically send a message to a channel.

var app = require('express').createServer()
  , io = require('socket.io').listen(app)
app.listen(80);
app.get('/:channel/:message', function (req, res) {
  //Code to create socket
  socket.emit("sent from get", {channel:req.params.channel, message:req.params.message})
});

io.sockets.on('connection', function (socket) {
  socket.on('sent from get', function (data) {
    socket.broadcast.to(data.channel).emit('channel message', { message: data.message});
  });
});

How to I create (and destroy) a socket connection in the app.get block?

(For clarity, I want to use this to send a quick message from a rails server when a particular object is saved, and have a message pushed to each appropriate user.)

2 Answers 2

1
io.sockets.in(req.params.channel).emit("channel message", {mes:req.params.message})

That will send a message to all users in the requested channel.

Sign up to request clarification or add additional context in comments.

Comments

0
var chat = io.of('/chat').on('connection', function (socket) {
socket.emit('a message', { that: 'only', '/chat': 'will get' });
chat.emit('a message', { everyone: 'in', '/chat': 'will get' }); });

The following example defines a socket that listens on '/chat'

Comments

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.