Perhaps this will help you too. I was having trouble getting my head wrapped around how socket.io works, so I tried to weld the example as much as possible.
I adapted this example from the example given here: http://socket.io/get-started/chat/
First, run in an empty directory and create a very simple file called package.json . Put the following into it.
{ "dependencies": {} }
Next, on the command line, use npm to install the dependencies that we need for this example.
$ npm install
This may take several minutes depending on the speed of your network connection / CPU / etc. To verify that everything went as planned, you can view the package.json file again.
$ cat package.json { "dependencies": { "express": "~4.9.8", "socket.io": "~1.1.0" } }
Create a file called server.js . This will obviously be our server running node. Paste the following code into it:
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){
Create the last file called index.html and put the following code in it.
<html> <head></head> <body> <div id="message"></div> <script src="/socket.io/socket.io.js"></script> <script> var socket = io(); socket.on('message', function(msg){ console.log(msg); document.getElementById("message").innerHTML = msg; }); </script> </body> </html>
Now you can test this very simple example and see some results similar to the following:
$ node server.js listening on *:3001 0.9575486415997148 0.7801907607354224 0.665313188219443 0.8101786421611905 0.890920243691653
If you open a web browser and point to the host name on which the node process is running, you should see that the same numbers are displayed in your browser along with any other connected browser that looks at the same page.
ray_voelker Oct 21 '14 at 19:37 2014-10-21 19:37
source share