Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use socket.io inside a express routes file

I'm trying to use Socket.io with Node.js and emit to a socket within the logic of a route.

I have a fairly standard Express 3 setup with a server.js file that sits in the route, and then I have an index.js which sits in a routes folders that exports all the pages/publically accessible functions of the site. So they look like:

exports.index = function (req, res) {     res.render('index', {         title: "Awesome page"     }); };  

with the routing defined in server.js like:

app.get('/',routes.index); 

I'm assuming I have to create the socket.io object in the server.js, since it needs the server object, but how can I access that object and emit to it from the index.js export functions?

like image 567
Matthew Arkin Avatar asked Sep 17 '13 17:09

Matthew Arkin


People also ask

Can you use Socket.IO with Express?

Socket.IO can be used based on the Express server just as easily as it can run on a standard Node HTTP server. In this section, we will fire the Express server and ensure that it can talk to the client side via Socket.IO.

Can I use Socket.IO without node js?

Is it possible to use socket.io without any node. js dependencies? The short answer is yes. You will however have Flash dependency.

How does Socket.IO work internally?

A simple HTTP handshake takes place at the beginning of a Socket.IO connection. The handshake, if successful, results in the client receiving: A session id that will be given for the transport to open connections. A number of seconds within which a heartbeat is expected ( heartbeat timeout )


2 Answers

There is a better way to do this now with Express 4.0.

You can use app.set() to store a reference to the io object.

Base configuration:

var app = require('express')(); var server = app.listen(process.env.PORT || 3000); var io = require('socket.io')(server); // next line is the money app.set('socketio', io); 

Inside route or middleware:

exports.foo = function(req,res){     // now use socket.io in your routes file     var io = req.app.get('socketio');     io.emit('hi!'); } 

Information about app.set() and app.get() is below:

app.set(name, value)

Assigns setting name to value. You may store any value that you want, but certain names can be used to configure the behavior of the server. These special names are listed in the app settings table.

Calling app.set('foo', true) for a Boolean property is the same as calling app.enable('foo'). Similarly, calling app.set('foo', false) for a Boolean property is the same as calling app.disable('foo').

Retrieve the value of a setting with app.get().

Source: https://expressjs.com/en/api.html#app.set

like image 144
aarosil Avatar answered Sep 23 '22 02:09

aarosil


You can set up your routes file as a function, and pass the Socket.IO object when requiring the file.

module.exports = function(io) {   var routes = {};   routes.index = function (req, res) {     io.sockets.emit('payload');     res.render('index', {       title: "Awesome page"     });   };   return routes; }; 

Then require routes like this:

var express = require('express'); var app = express(); var http = require('http'); var server = http.createServer(app); var io = require('socket.io').listen(server); var routes = require('./routes')(io); 
like image 41
hexacyanide Avatar answered Sep 21 '22 02:09

hexacyanide