Skip to content

Commit

Permalink
UPDATED: websocket-over-nodejs and socketio-over-nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
muaz-khan committed Apr 22, 2019
1 parent ca0f0ef commit 8497675
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 152 deletions.
145 changes: 3 additions & 142 deletions socketio-over-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,121 +8,14 @@
<img src="https://nodei.co/npm/socketio-over-nodejs.png">
</a>

=
#### `server.js` file

#### Install via `npm`
This is the file that I was running on `https://webrtcweb.com:9559/`. I'll recommend to modify and use this file.

```
npm install socketio-over-nodejs
```

and run the `signaler.js` nodejs file:

```
// run simple HTTP server
node ./node_modules/socketio-over-nodejs/signaler.js
// run HTTPs server
node ./node_modules/socketio-over-nodejs/signaler-ssl.js
```

Now you can test: `http://localhost:8888/` or `https://localhost:8888/`

You can use ip-address `127.1.1` on Mac/Linux instead of `localhost`.

=

### Install on Linux/Ubuntu/CentOS/Debian/Mac etc.

```
# create a directory
mkdir socketio-over-nodejs
# open directory
cd socketio-over-nodejs
# get package
wget http://webrtcweb.com/socketio-over-nodejs.tar
# extract package
tar -xf socketio-over-nodejs.tar
# run simple node.js server
node signaler.js
# run HTTPs server
node signaler-ssl.js
```

# Download ZIP on windows

http://webrtcweb.com/websocket-over-nodejs.zip

Now, you can open port `8888` on your ip address/domain; or otherwise on localhost: `http://localhost:8888/`

=

It is using port `8888`; you can edit this port using following commands:

```
vi signaler.js
# now edit port 8888
# and save changes & quit
# press "insert" key; then press "Esc" key and the type
:wq
```

`:wq` command saves changes in the file and exits editing mode. If you don't want to save changes; simply type:

```
# if you don't want to save changes however want to exit editing mode
:q
```

Common Error: `Error: listen EADDRINUSE`. It means that same port is used by another application. You can close all existing processes running on same port:

```
// list all active processes running on same port
fuser -v 8888/tcp
// kill all processes running on port "8888"
fuser -vk 8888/tcp
// list again to verify closing ports
fuser -v 8888/tcp
```

You can delete "directory" and re-install:

```
rm -rf socketio-over-nodejs
mkdir socketio-over-nodejs
# and following above steps to "wget" and "tar" then "node" to run!
```

=

1. https://www.webrtc-experiment.com/docs/WebRTC-Signaling-Concepts.html
2. http://www.RTCMultiConnection.org/FAQ/
3. http://www.RTCMultiConnection.org/docs/sessionid/
4. http://www.RTCMultiConnection.org/docs/channel-id/

=

Otherwise, follow these steps:

1. Download and extract **ZIP file** of this repository then copy `folder-location` of the`signaler.js` file
2. Open **Node.js command prompt** window
3. Type command `cd folder-location` where `folder-location` can be `C:\socketio-over-nodejs`
4. Type `npm install express` or [download ZIP](http://code.snyco.net/node_modules/express.zip)
5. Type `npm install socket.io` or [download ZIP](http://code.snyco.net/node_modules/socket.io.zip)
6. Type `node signaler` to run the node.js server

Then open `http://localhost:8888/`.

# How to use?

In `ui.js` files you can find `openSocket` method; or in all libraries; you can find `openSignalingChannel` method.
Expand Down Expand Up @@ -211,38 +104,6 @@ function testChannelPresence(channel) {
testChannelPresence('default-channel');
```

# What is `cd folder-location`?

Using this command; you can open project's directory (i.e. folder).

# What is `node signaler`?

This command runs node.js server via `signaler.js` file. That file handles socket.io relevant stuff.

# Note

Each experiment is using something like this:

```javascript
var SIGNALING_SERVER = '/';
```

This is the URL of your site. By default it will be equal to `http://localhost:8888/`.

It is strongly recommended to use absolute URL including port number:

```javascript
var SIGNALING_SERVER = 'http://domain.com:8888/';
```

# Signaling Concepts

Interested to understand WebRTC Signaling Concepts? Read [this document](https://github.com/muaz-khan/WebRTC-Experiment/blob/master/socketio-over-nodejs/Signaling-Concepts.md).

# SSL/TLS/HTTPS/Socket.io/Node.js ??

https://github.com/muaz-khan/WebRTC-Experiment/issues/62

# License

[Socket.io over Node.js](https://github.com/muaz-khan/WebRTC-Experiment/blob/master/socketio-over-nodejs) is released under [MIT licence](https://www.webrtc-experiment.com/licence/) . Copyright (c) [Muaz Khan](https://plus.google.com/+MuazKhan).
[Socket.io over Node.js](https://github.com/muaz-khan/WebRTC-Experiment/blob/master/socketio-over-nodejs) is released under [MIT licence](https://www.webrtc-experiment.com/licence/) . Copyright (c) [Muaz Khan](https://MuazKhan.com).
9 changes: 4 additions & 5 deletions socketio-over-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "socketio-over-nodejs",
"subdomain": "socketio-over-nodejs",
"preferGlobal": true,
"version": "3.0.2",
"version": "3.0.3",
"author": {
"name": "Muaz Khan",
"email": "[email protected]"
Expand All @@ -15,9 +15,9 @@
}
],
"scripts": {
"start": "node signaler.js"
"start": "node server.js"
},
"main": "signaler.js",
"main": "server.js",
"repository": {
"type": "git",
"url": "https://github.com/muaz-khan/WebRTC-Experiment.git"
Expand Down Expand Up @@ -46,8 +46,7 @@
"node": ">=0.6"
},
"dependencies": {
"socket.io": "0.9.x",
"node-static": "0.7.x"
"socket.io": "0.9.x"
},
"bugs": {
"url": "https://github.com/muaz-khan/WebRTC-Experiment/issues"
Expand Down
99 changes: 99 additions & 0 deletions socketio-over-nodejs/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// https://www.webrtc-experiment.com/

var fs = require('fs');

// don't forget to use your own keys!
var options = {
// key: fs.readFileSync('fake-keys/privatekey.pem'),
// cert: fs.readFileSync('fake-keys/certificate.pem')
key: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/fullchain.pem')
};

// HTTPs server
var app = require('https').createServer(options, function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
var link = 'https://github.com/muaz-khan/WebRTC-Experiment/tree/master/socketio-over-nodejs';
response.write('<title>socketio-over-nodejs</title><h1><a href="'+ link + '">socketio-over-nodejs</a></h1><pre>var socket = io.connect("https://webrtcweb.com:9559/");</pre>');
response.end();
});


// socket.io goes below

var io = require('socket.io').listen(app, {
log: true,
origins: '*:*'
});

io.set('transports', [
// 'websocket',
'xhr-polling',
'jsonp-polling'
]);

var channels = {};

io.sockets.on('connection', function (socket) {
var initiatorChannel = '';
if (!io.isConnected) {
io.isConnected = true;
}

socket.on('new-channel', function (data) {
if (!channels[data.channel]) {
initiatorChannel = data.channel;
}

channels[data.channel] = data.channel;
onNewNamespace(data.channel, data.sender);
});

socket.on('presence', function (channel) {
var isChannelPresent = !! channels[channel];
socket.emit('presence', isChannelPresent);
});

socket.on('disconnect', function (channel) {
if (initiatorChannel) {
delete channels[initiatorChannel];
}
});
});

function onNewNamespace(channel, sender) {
io.of('/' + channel).on('connection', function (socket) {
var username;
if (io.isConnected) {
io.isConnected = false;
socket.emit('connect', true);
}

socket.on('message', function (data) {
if (data.sender == sender) {
if(!username) username = data.data.sender;

socket.broadcast.emit('message', data.data);
}
});

socket.on('disconnect', function() {
if(username) {
socket.broadcast.emit('user-left', username);
username = null;
}
});
});
}

// run app

app.listen(process.env.PORT || 9559);

process.on('unhandledRejection', (reason, promise) => {
process.exit(1);
});

console.log('Please open SSL URL: https://localhost:'+(process.env.PORT || 9559)+'/');
10 changes: 10 additions & 0 deletions websocket-over-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

[![npm](https://img.shields.io/npm/v/websocket-over-nodejs.svg)](https://npmjs.org/package/websocket-over-nodejs) [![downloads](https://img.shields.io/npm/dm/websocket-over-nodejs.svg)](https://npmjs.org/package/websocket-over-nodejs)

#### `server.js` file

This is the file that I was running on `wss://webrtcweb.com:9449/`. I'll recommend to modify and use this file.

```sh
npm install ebsocket-over-nodejs
```

#### Explanation

This repository has following kinds of browser-based demos:

1. Text Chat using rooms
Expand Down
4 changes: 2 additions & 2 deletions websocket-over-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "websocket-over-nodejs",
"preferGlobal": true,
"version": "0.0.4",
"version": "0.0.6",
"author": {
"name": "Muaz Khan",
"email": "[email protected]"
Expand Down Expand Up @@ -36,7 +36,7 @@
"bugs": {
"url": "https://github.com/muaz-khan/WebRTC-Experiment/issues"
},
"homepage": "https://webrtcweb.com:9449/",
"homepage": "https://github.com/muaz-khan/WebRTC-Experiment/tree/master/websocket-over-nodejs",
"_id": "websocket-over-nodejs@",
"_from": "websocket-over-nodejs@"
}
5 changes: 2 additions & 3 deletions websocket-over-nodejs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ var fs = require('fs');
var options = {
// key: fs.readFileSync('fake-keys/privatekey.pem'),
// cert: fs.readFileSync('fake-keys/certificate.pem')
key: fs.readFileSync('/home/ssl/webrtcweb.key'),
cert: fs.readFileSync('/home/ssl/server.csr'),
ca: fs.readFileSync('/home/ssl/inter.csr')
key: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/fullchain.pem')
};

// HTTPs server
Expand Down

0 comments on commit 8497675

Please sign in to comment.