I am trying to create a port over at localhost:3000 but it doesn't work. If I visit localhost:3000 from my browser, I get a 404.
I know the problem. I haven't even opened a port. I just randomly put that port number 3000 there. I don't even know.
var socket = require( 'socket.io' );
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen( server );
var port = process.env.PORT || 3000;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
io.on('connection', function (socket) {
socket.on( 'new_count_message', function( data ) {
io.sockets.emit( 'new_count_message', {
new_count_message: data.new_count_message
});
});
socket.on( 'update_count_message', function( data ) {
io.sockets.emit( 'update_count_message', {
update_count_message: data.update_count_message
});
});
socket.on( 'new_message', function( data ) {
io.sockets.emit( 'new_message', {
name: data.name,
email: data.email,
subject: data.subject,
created_at: data.created_at,
id: data.id
});
});
});
I honestly don't know how to open a port with MAMP. Please walk me through guys. I have spent the past 3 hours researching on web sockets. All I get it how to set them them up in PHP, Java, etc, but they don't say how to actually create the port.
Any help is highly appreciated. Thanks!
You don't actually create a port using MAMP.
So in your case, var port = process.env.PORT || 3000; you are listening to port 3000.
If you visit localhost:3000 you will get a 404. The code above, you could save that to your root directory of your server in a js file. In this e.g., let's call it server.js. After you save it, all you need to do is go to your terminal, and type cd to your directory, and then type in node server.js. In your case, you should get a message saying Server listening at port 3000.
Now, if you visit localhost:3000, you should be able to see the page.
Hope that helps!
If you're using MAMP Pro, then you can change the default Apache port for HTTP connection or https connection from the General options. However, if you're using MAMP (free), then you can modify the ports in the application's Preferences Panel.
You can find more about how to change the default port on MAMP from the official documentation: https://www.mamp.info/en/documentation/
Related
I have a PHP application which uses websockets,
When I run it on my local, it works fine, but when I deploy it on my web host, it doesn't anymore.
I am on cloud-web-1 of OVH.
I saw that this plan supports websockets.
When I launch the PHP server script with SSH, it works fine, but on the client side, when I create a js object socket with url of the websocket ws://ip-address:port or wss or ws://my-domain:port , it never work and it always show error on the browser console like "Connection to websoccket failed.",
Help me please,
Thanks
On the server side (your PHP code), ensure your websocket is listening on http://0.0.0.0:<port> (I tried with port 3000).
On the HTML side, here is an example of connection to your websocket:
<script src="/socket.io/socket.io.js"></script>
<script>
var host = location.protocol + '//' + location.host;
var socket = io.connect(host);
socket.on('news', function (data) {
<your code>
}
</script>
I'm currently trying to host my own WebSocket server using Ratchet http://socketo.me/docs/push.
The problem is that I can't find a good tutorial that shows me how I can host this on a subdomain. So hopefully someone can help me here.
My plan:
I already have a basic auth secured subdomain called ws.my-domain.de. Now I want to run Ratchet on my subdomain to provide this as a service for my main domain and all my subdomains.
At my main domain my-domain.de I've WordPress running so this is where I want to use my own WebSocket first via the client side tutorial from the page I've posted above:
<script src="https://gist.githubusercontent.com/cboden/fcae978cfc016d506639c5241f94e772/raw/e974ce895df527c83b8e010124a034cfcf6c9f4b/autobahn.js"></script>
<script>
var conn = new ab.Session('ws://ws.my-domain.de',
function() {
conn.subscribe('kittensCategory', function(topic, data) {
// This is where you would add the new article to the DOM (beyond the scope of this tutorial)
console.log('New article published to category "' + topic + '" : ' + data.title);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>
So can please someone show me the steps I need to do? I'm completely new with this. I know how to use it on the client side, but I don't know how to provide it as a service and then use it in PHP (WordPress).
I'm assuming you are on a VPS or dedicated server.
You need to run your WebSocket as a daemon and then create a vhost for your subdomain in Nginx or Apache and using that create a reverse proxy to your WebSocket server.
Running ratchet as a daemon
Nginx WebSocket reverse proxy
Apache WebSocket reverse proxy
I want to use socket.io in codeigniter and I have followed all the process like install express and socket.io in root and I also have created the server.js file bellow
server.js
var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Server listening at port %d', port);
});
io.on('connection', function(socket) {
socket.on('new_message', function(data) {
io.sockets.emit('new_message', {
message: data.message,
date: data.date,
msgcount: data.msgcount
});
});
});
When I run this server.js in terminal, it outputs 'Server listening at port 3000',
but when I request localhost:3000 in browser, it shows Cannot GET /. I do not understand this error.
You are adressing http request to express router which is not set in your code. Socket.io is sending xhr or socket requests and does not handle http requests. Either use online services like Socket echo or raise router which will receive your browser request and echo it it via socket to your socket.io instance.
We have existing laravel application running on VPS provider on ubuntu with apache, we have to implement real-time features through node app (socket.io and express) seems fine in local environment but when i try it to production it doesnt working at all, my question is, what is the proper configuration i will be made in order to run my node app like $ node server.js
snippet code(server.js).
var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3001;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
io.on('connection', function(socket) {
code here....
});
snippet code(client-side(js-file)).
var socket = io();
ERROR
GET https://hostname/socket.io/?EIO=3&transport=polling&t=1480917572919-5 404 (Not Found)
snippet code(client-side(js-file)).
var socket = io() ;
to
var socket = io.connect('http://localhost:3001');
I'm trying to setup a node.js socket.io server, and it's not working. It seems like it's trying to poll to the node.js server first, but because I'm using Laravel for the majority of my app, that's picking it up and returning a Not FoundHttpException
Node.js server code:
var app = require('express')();
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(channel, message);
io.emit('foo', message.data);
});
io.on('connection', function() {
console.log('foo');
});
Client code:
var socket = io.connect('http://myapp.app');
socket.on('foo', function(data) {
console.log(data);
});
All this does is triggers the following HTTP long polling requests in my client:
http://myapp.app/socket.io/?EIO=3&transport=polling&t=1447468676627-0
which responds with:
Server 500 error, #29 Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /home/vagrant/myapp/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:161
Well of course it's not found. I'm expecting socket.io to attempt to create a websocket connection - not send long-polling HTTP requests to an undefined Laravel route. What am I doing wrong?
You don't actually need express here, as you are not displaying anything, just simple http server.
I think your issue, that the server does not listen.
Also node should listen to different port than 80, otherwise your http server will handle the request and lead to that 500 error.
Check code below.
Server side
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
app.listen(3000);
Client side
var socket = io('http://localhost:3000');
Make sure you run node from CLI
node index.js
And replace index.js with your script name.