“iSamepi”

目指せ情強。映画好きの情弱がいろいろ頑張って調べたことをまとめるブログ。

node.js + socket.io

How to use node.js and socket.io on Linux or ARM devices.

1. on Linux
1.1 install node.js and socket.io
Using the distribution of linux is CentOS 6.5.
To open terminal window, and

> yum install node.js

may be on the other distributions, GET can be used.
after while, installation will be over.

First, what is the node.js.
node.js is the interpreter of javascript.
It means the fact to enable to use "server-side javascript".

javascript is known as the script language runs on web browser.
node.js will be able to use javascript programs on servers.

but node,js is only the interpreter.
what negotiation makes is websocket.

to use websocket, we need the other modules on node.js.
It names Socket.io.
Socket.io makes possible to negotiate between clients - server using websocket.

To use Socket.io on node.js, you can use this command.

> npm install -g socket.io

this command is to install globally in your device.
To install locally, without -g .

npm means node package manager will be installed automatically with 
installing node.js.

1.2 To use node.js and socket.io
this section assume that creating some kind of chat apps.

chat apps will contain follow functions.
- server side
massages send to / receive from client
conference room ( don't send unexpected members )

 - client side
massages send to / receive from server

1.3 APIs of Socket.io
Socket.io contains follow APIs. Mainly, 2 APIs are important.

> emit('event', 'message');
Send message. 
> on('event', function('message'));
Receive message.

ex)
client
> emit('greeting', 'hello world');

server
> on('greeting', function(){
   console.log( message )
});

then server console will show

opposite case works as well.

please attention follows.
emit is send message all clients connect to the same server.

client ----(emit)----> server

server -----(emit)----> all clients

next entry, i write more about this.