4

I have a basic front-end (html, css, jquery) and I'd like to use sails.io.js to communicate with an API server (developped with sails, with cors enabled). The API is running on localhost:10000 but it will be on an another domain than the one of the webclient later on.

Directly from jquery, I can issue some get request to this API and get the expected results.

When it comes to websocket, I have some problems... In the index.html (just to test), I put the following:

<script src="js/sails.io.js"></script>
<script type="text/javascript">
    io.sails.url('http://localhost:10000');
    io.socket.get('/data', function serverResponded (body, sailsResponseObject) {
      // body === sailsResponseObject.body
      console.log('Sails responded with: ', body);
      console.log('with headers: ', sailsResponseObject.headers);
      console.log('and with status code: ', sailsResponseObject.statusCode);
    });
 </script>

But Chrome's developer tools tell me

ReferenceError: io is not defined 

Any idea ?

UPDATE

I'm serving index.html with a web server (python -m SimpleHTTPServer)
I've installed sails.io.js using bower.

I've try to make this test as simple as possible:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body>
    <script src="bower_components/sails.io.js/dist/sails.io.js"></script>
    <script src="index.js"></script>
  </body>
</html>

index.js:

window.onload=function(){
    io.sails.url = 'http://localhost:10000';
    io.socket.get('http://localhost:10000/data', function (body, response) {
      console.log('Sails responded with: ', body);
    });
};

My sails (0.9.16) API is only returning a json object on the GET /data route.

I have implemented a dummy __getcookie function in the api:

'get /__getcookie': function(req, res, next){
    res.json({ok: 123});
}

And commented the line 481 in interpret.js (Scott comments below).

I have also modify config/socket.js with:

 authorization: false,

=> I can now get the result from the /data route of my API :)

But... on each request I have the following error:

error: Error: No valid session available from this socket.
Luc
  • 16,604
  • 34
  • 121
  • 183

2 Answers2

3

First of all, sails.io.js includes the code for socket.io.js, so there is no need to try and include that separately. You should remove this line:

<script src="bower_components/socket.io/lib/socket.js"></script>

Next, if you're just loading index.html from disk (rather than serving it from a web server), you'll need to tell the Sails socket client what URL to connect to:

io.sails.url = 'http://localhost:10000';

Put this anywhere before you start making socket calls; the library is smart enough to wait until its connected before trying to make the calls. So, altogether:

window.onload=function(){
    io.sails.url = 'http://localhost:10000';
    io.socket.get('http://localhost:10000/data', function (body, sailsResponseObject) {
        console.log('Sails responded with: ', body);
        console.log('with headers: ', sailsResponseObject.headers);
        console.log('and with status code: ', sailsResponseObject.statusCode);
    });
};

should work. You should be able to see in the console whether or not the socket connected by looking for the "io.socket connected successfully." message.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • I've done exaxtly what you described but still have some errors (I've updated the question). – Luc Jun 27 '14 at 04:46
  • You're not doing what I described. You're calling `url` as a function instead of setting the var. On my end, I had a typo in my answer (it's `io.socket`, not `io.sails`). – sgress454 Jun 27 '14 at 05:00
  • Sorry, my bad. This time it's exactly as you suggested :) but I end up with this: GET /socket.io/1/?__sails_io_sdk_version=0.10.0&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&t=1403848955182 HTTP/1.1" 404 - – Luc Jun 27 '14 at 06:03
  • Just a though... could this error be linked to the fact I'm using sails.io.js 0.10.1 (default version installed by bower) where my sails API server is running 0.9.16 ? – Luc Jun 27 '14 at 07:09
  • Eek, this is embarrassing but I was right the first time: it's `io.sails.url`, not `io.socket.url`. Otherwise it should work, and the `sails.io.js` from Bower should be backwards-compatible with v0.9.16. I don't get a 404 when I try it... – sgress454 Jun 27 '14 at 19:42
  • I've updated the question and tried to use the simplest configuration possible. It seems the solution is getting closer, but still a new error message related to __getcookie method. Any idea ? – Luc Jun 27 '14 at 22:08
  • Yep, that route is only available in Sails v0.10.x. It's a way for the app to deliver a 3rd party cookie so that the socket can use sessions. You can easily add a `get /__getcookie` route to your app; all it needs to do is return a valid response. I've also noticed a bug in the v0.9.16 session handling which can prevent 3rd-party sockets from accessing sessions; the easiest way around it is to [comment out in this line in the socket interpreter](https://github.com/balderdashy/sails/blob/stable/lib/hooks/sockets/interpreter/interpret.js#L481). – sgress454 Jun 28 '14 at 23:01
  • I've commented the line in interpret.js. Could you confirm this problem with session/cookie will not prevent me from keeping track of the opened socket (the purpose of all of this is to be able to send an event (from the server) to some clients sockets when a database insert is done (I'm using mongo-watch to check the db)). Regarding __getcookie method, what should this guy return (only a json object) ? – Luc Jun 29 '14 at 12:51
  • I've made a new update to the question with the results I got after your suggestions. I also had to use 'authentication: false' in config/socket.js (not sure this is the good way to go though) – Luc Jun 29 '14 at 13:18
  • `authorization: false` is going to skip dealing with cookies, so you won't have a valid session. – sgress454 Jun 29 '14 at 16:29
  • 'authorization: true' gave me an error. What should __getcookie method return exactly ? While 'authorization: false' is working fine, I still have the following error each time a client makes a request 'Error: No valid session available from this socket'. Any idea how to solve this ? – Luc Jun 30 '14 at 07:33
  • I've started having an issue with my ionic app not making socket connection to my Sails v0.11.0 API. @sgress454 you mention commenting out a line in the socket interpreter. But the link is broke. – Brad W Oct 05 '15 at 20:50
0
  1. did you try with a / in front of the src, like:

    < script src="js/sails.io.js">

  2. Do you have the sails.io.js in the /assets/js/ folder (sails 0.10) or in the /assets/linker/js folder (sails 0.9 and below).

  3. Did sails lift copied that js file to .tmp/public/js folder?

  4. Where is your index.html file located?

Diego Pamio
  • 1,377
  • 13
  • 21
  • 1. I tried with / without best results. 2. I only use sails.io.js on its own. 4. index.html is just a test file on its own – Luc Jun 26 '14 at 19:13
  • Mine is in `asseets/js/dependencies` (v0.11.0), but `http://localhost:1337/js/dependencies/sails.io.js` returns a `404` while accessing from `/signup` view (``). What's up with that? – Cody Jun 19 '15 at 03:55