5

I gonna to make a simple WebSocket application waiting the clients to connect to. The clients would be Android users, and the application itself is a simple chat for two peoples. So for the Android application I need to know WebSocket address (starts with ws:// or wss://). I already have some website, where I install nodejs. But after couple of days I completely stuck what's going on and how make something work. I would be even glad to see nodejs catches any WebSocket messages and that's it.

I read some manuals about nodejs and socket.io, and again I have no idea where to get that ws:// address and make it work somehow.

For example, from socket.io chat manual, we have:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

Is that already simple websocket application? Where is my desired ws:// address in that case? Maybe should I just upload that bunch of code on my server and send queries to ws://mydomain.com:3000 ?

James May
  • 1,371
  • 3
  • 20
  • 37

1 Answers1

4

If you're using the socket.io library on the server, then you have to use a socket.io compatible library in the client too because it is a protocol on top of webSocket so both ends must speak the same language.

webSockets (and socket.io in this case) are built to share your webserver. So, any webSocket connect request to your server is just sent to the same host and port as your webserver.

Each webSocket request starts with an http request (thus it is handled by your web server) and that http request as a header in it that requests an upgrade to the webSocket protocol. When your web server sees that custom header, it passes off control to the socket.io library which then responds that the upgrade to the webSocket protocol is OK and the socket which started out life as an http request, now becomes the socket over which the app speaks webSocket/socket.io.

The socket.io library handles plugging into your web server automatically in order to see these protocol upgrade requests.

In the webpage, the socket.io client code to connect to this would just look like this:

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

var socket = io();  
socket.on('connect', function() {
    // socket.io webSocket connection with your server is now established
});

</script>

The socket.io library will automatically connect back to the same host that the web page came from. You don't even need to put in an URL.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you. But what if I have Android application and it's not placed on the same server (somewhere in Placemarket I think ). I guess socket.io is not the right choise for this purpose, but what's the right? – James May May 13 '15 at 17:32
  • There is a library that you can use for Android socket.io client https://github.com/nkzawa/socket.io-client.java . Check out this post: http://socket.io/blog/native-socket-io-and-android/ – jmartins May 13 '15 at 17:34
  • @JamesMay - You can likely find a socket.io compatible client library for Android if you want. I find socket.io makes webSockets quite a bit easier to use so that may be a good path. How exactly you program to a socket.io compatible Android library would depend entirely on which library you use and how it works. The socket.io site appears to be having problems at the moment, but they have a discussion of native android apps here: http://socket.io/blog/native-socket-io-and-android/ – jfriend00 May 13 '15 at 17:34