i have socket io redis and express pubsub going on, in my local host it works just fine i am using laravel events to publish to redis then socket io is supposed to emit the message, its working okay on local host but on my production server which is an ec2 vm it only gets to connect and i can see a console message 'connected' but it does not emit any events even though messages are being published to redis here is my client and server
//client
<script type="text/javascript">// <![CDATA[
var socket = io.connect('http://127.0.0.1:3000/');
socket.on('connect', function(data){
//socket.emit('subscribe', {channel:'score.update'});
console.log('connected');
});
socket.on('notification.update', function (data) {
//Do something with data
console.log('Notification Caught: ', data);
});
// ]]></script>
//server
var express = require('express'),
http = require('http'),
server = http.createServer(app);
var app = express();
const redis = require('redis');
const io = require('socket.io');
const client = redis.createClient();
server.listen(3000, 'localhost');
io.listen(server).on('connection', function(client) {
const redisClient = redis.createClient()
redisClient.subscribe('notification.update');
});
redisClient.on("message", function(channel, message) {
//Channel is e.g 'score.update'
client.emit(channel, message);
});
client.on('disconnect', function() {
redisClient.quit();
});
});
You need to change the listening of your server:
Change these:
var express = require('express'),
http = require('http'),
server = http.createServer(app);
var app = express();
const redis = require('redis');
const io = require('socket.io');
const client = redis.createClient();
And
server.listen(3000, 'localhost');
io.listen(server).on('connection', function(client){
//Code
});
To:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
And
io.on('connection, function(client){
//Code
});
server.listen(3000, function(){
console.log("Listening on :3000");
});
Related
I created node js https server by nodejs.
var express = require('express');
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('privatekey.pem'),
cert: fs.readFileSync('certificate.pem')
};
var app = express();
var server = https.createServer(options, app).listen(443,()=>{
console.log('listen to https');
});
var io = require('socket.io').listen(server);
io.on('connection', function (socket) {
console.log(' user connected');
socket.on('disconnect', function () {
console.log('a user disconnected');
});
});
after start server, when i want to connect to server by socket.io client tools like : https:///172.16.130.3:443 i got :
Connection to https:///172.16.130.3:443 timed out!
Im setting up a chat server with socket.io. I followed their tutorial til it came to SSL. I found some explainations here on stack and tutorials in the net, so i came up with the following code.
I replaced my real domain with "my-domain.de"
var app = require('express')();
var fs = require('fs');
var https = require('https');
var io = require('socket.io')(https);
var mysql = require('mysql');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/chain.pem')
};
var serverPort = 8443;
var server = https.createServer(options, app);
server.listen(serverPort, function(){
console.log('listening on *:' + serverPort);
});
io.on('connection', function(socket){
console.log('a user connected');
});
The Server runs, but the Client doesnt recieve the socket.io.js. So i started the server with DEBUG=* -node index.js to see whats happening. It shows the following Error when a client tried to connect:
express:application no routes defined on app +9s
finalhandler default 404 +0ms
The Client looks like this:
<script src="https://my-domain.de:8443/socket.io/socket.io.js"></script>
<script>
$(function () {
var socket = io('https://my-domain.de:8443/');
</script>
After hours of headache i found the solution and it is as simple as always...
I simply had to place var io = require('socket.io')(server); after var server = https.createServer(options, app); So that it can route correctly...
So that my final server code looks like this:
var app = require('express')();
var fs = require('fs');
var https = require('https');
var mysql = require('mysql');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/chain.pem')
};
var serverPort = 8443;
var server = https.createServer(options, app);
var io = require('socket.io')(server); // HERE TO PUT THE DAMN SOCKET.IO!!
server.listen(serverPort, function(){
console.log('listening on *:' + serverPort);
});
io.on('connection', function(socket){
console.log('a user connected');
});
Good morning why this code don't work (server respond but "io.on" no):
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//this code not work in console // even it can not see
io.on('connection', function(socket) {
console.log('a user connect');
socket.on('disconnect', function() {
console.log('a user disconnect');
});
});
//server respond
http.listen(3000, function() {
console.log('Server running at 3000');
});
I'm a beginner, express and socket.io i downloaded by npm
You need to have a server as well as a client which will make a connection to the socket io server.
Based on what I understood from your code, I guess you want to do something like this:
First your server.js:
#!/usr/bin/env node
var http = require('http');
var express = require('express');
var app = express();
var io = require('socket.io');
var port = 3000;
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
var socketio = io.listen(server);
setInterval(function() {
console.log("Emitting..");
socketio.emit('data', {"x":"123"});
}, 2000);
module.exports = app;
Then your client.html: (I am writing a client, which will log into browser console.)
<html>
<head></head>
<body>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
var socket = io.connect("http://localhost:3000/");
socket.on('connect', function() {
console.log('connected');
});
socket.on('disconnect', function(){
console.log('disconnected');
});
socket.on("data", function(data) {
console.log(data);
});
</script>
</body>
That's it. Now start your server with node server.js and then, open up client.html in your browser and check console. Let me know, if there's anything unclear.
No idea why this is happening. The socket.io client is trying to connect on the front end:
from https://mywebsite.com:3000, But no response is being received from my node.js server:
var app = require('express')();
var cors = require('cors');
app.use(cors());
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('ioredis');
var Redis = new redis();
Redis.subscribe('live-updates');
Redis.on('message', function(channel, message) {
message = JSON.parse(message);
console.log(message);
io.emit(channel + ':' + message.event, message.data);
});
server.listen(3000, function() {
console.log('listening');
});
And the node server is running and listening successfully in the background using forever. Yet, there's no handshaking occurring. What's going on here?
Does the fact that I'm using SSL have something to do with this?
Edit
I have updated my node.js server configuration the the following, and am still stuck where I began.
var app = require('express')();
var cors = require('cors');
app.use(cors());
var https = require('https');
var fs = require('fs');
var io = require('socket.io')(https);
var redis = require('ioredis');
var Redis = new redis();
Redis.subscribe('live-updates');
Redis.on('message', function(channel, message) {
message = JSON.parse(message);
console.log(message);
io.emit(channel + ':' + message.event, message.data);
});
var options = {
key: fs.readFileSync('/etc/nginx/ssl/mysite.com/17710/server.key'),
cert: fs.readFileSync('/etc/nginx/ssl/mysite.com/17710/server.crt')
};
https.createServer(options, app).listen(3000, function() {
console.log('listening');
});
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
server.listen(3000);
var io = require('socket.io').listen(server);
io.set('log level', 1);
io.sockets.on('connection', function(socket) {
console.log('connected');
});
after running
node server.js
sometimes 'connected' is logged to console without actual connection where does it come from?