user5616430
user5616430

Reputation: 13

How to open web socket PORT with MAMP

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!

Upvotes: 1

Views: 2046

Answers (2)

mmik
mmik

Reputation: 5991

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/

Upvotes: 0

Taylor
Taylor

Reputation: 3141

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!

Upvotes: 2

Related Questions