简体   繁体   中英

Sails.js websockets work in localhost but not working after I deploy to Openshift

Summary:

I am working on an admin panel for a sails.js based website. I have configured websockets to work in my localhost. But when I deploy to openshift, only the io.socket.on('connect',...); is firing. But when I publishAdd a record, nothing is being received in the client.


Details:

Firstly some details about my configuration:

  • Sails.js version: 0.12.3
  • Node.js version: v0.10.36 (Changed it to this to match openshift's version v0.10.35)
  • Socket client: sails.io.js
  • Tried with both memory socket store and redis socket store and the behavior is the same

Current Behavior:

I am trying to enable a notification system to my application so I am using the default sails.io.js to connect a websocket (Using the default io.socket object) in my client side code I am using console.log() for now to log any events. The code looks like this in the client js file:

$(document).ready(function () {
  io.sails.environment = "development";

  if(window.location.hostname === "localhost") {
    io.sails.url = 'http://localhost:1337';
  } else {
    io.sails.url = 'http://myapp-username.rhcloud.com:8000';
  }

  var adminSocket = io.socket;

  // Access socket's connect event to initialize functions
  adminSocket.on('connect', function () {
    console.log("Connected to socket");
    // Subscribe to admin model of current user
    adminSocket.get('/admin/subscribeToModel');
  });

  // Access notification (adminAlerts) model's socket events
  adminSocket.on('admin', function(event) {
    console.log(event);
  });

  // Log Errors
  adminSocket.on('error', function (event) {
    console.log(event);
  });
});

As you can see the code runs io.socket.get(...) after the socket connects. This works for both localhost and openshift and logs "Connected to socket" and also sails.io.js's default connection message. When the server turns off the client tries to reconnect as the default behavior.

io.socket.get runs a custom controller function in the admin controller:

subscribeToModel: function(req, res, next) {
    if (!req.isSocket) {
      console.log("Not Socket");
      return res.badRequest();
    }
    console.log("Socket");
    Admin.subscribe(req, [req.session.userid]); //Subscribes to Admin model's entry with the user's id

    return res.json({message: "Subscribed"});
  }

This logs "Socket" in Both openshift and localhost but the return message is not available. I don't know if subscribe works or not in openshift but it works for sure in localhost.

This is the setup. Now I built a function to test socket behavior. In admin controller I created:

createAndBroadcastNotification: function (req, res, next) {
    AdminAlerts.create({
      admin: req.session.admin.id,
      alertHeader: 'Some Header',
      alertBody: 'Some Message',
      alertType: 'label-info',
      url: '/somepage'
    }).exec(function (err, alert) {
      if (err) {
        res.serverError(err);
      }
      Admin.findOne({ where: {id: req.session.admin.id} }).populate('alerts').exec(function (err, admin) {
                if (err) {
            res.serverError(err);
          }
                Admin.publishAdd(req.session.userid, 'alerts', alert.id);
                return res.json({message: "Done"});
        });
    });
  }

After this Both openshift and localhost are showing {message:Done} in the browser and the record is being created and associated in the mySQL database. But Only Localhost is posting this message in the console as expected:

Object {id: 1, verb: "addedTo", attribute: "alerts", addedId: 10}

Openshift is showing no such message. Neither is it showing any error messages.

I have been trying to figure out the problem for 5 days. I have not been able to find a clue as to why this is happening.


Objective:

I wish to get the console.log() message in the client to signify an alert being added.

I figured it out. For anyone facing the same or similar issue, the documentation for openshift (In the blog) is outdated. Letting sails.io.js figure out the host and port works "just fine". It returns a connection error in firefox though. Here are the changes:

// client-side-code.js
$(document).ready(function () {
  // io.sails.environment = "development";

  // if(window.location.hostname === "localhost") {
  //   io.sails.url = 'http://localhost:1337';
  // } else {
  //   io.sails.url = 'http://myapp-username.rhcloud.com:8000';
  // }

  var adminSocket = io.socket;

  // Access socket's connect event to initialize functions
  adminSocket.on('connect', function () {
    console.log("Connected to socket");
    // Subscribe to admin model of current user
    adminSocket.get('/admin/subscribeToModel');
  });

  // Access notification (adminAlerts) model's socket events
  adminSocket.on('admin', function(event) {
    console.log(event);
  });

  // Log Errors
  adminSocket.on('error', function (event) {
    console.log(event);
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM