Is there a way to establish a connection to MongoDB from a different device in order to access the chat app that is locally hosted

I have developed a basic chat application using sockets, MongoDB, and Express. Everything functions smoothly when the application is running on my local machine (localhost:4000). I can establish connections to MongoDB, send messages, and receive responses without any issues. Problem: However, there seems to be an issue when accessing the app through a mobile browser using the IP address of my PC (e.g., 192.168.1.108:4000). While I am able to view the index.html page, I encounter difficulties in sending and receiving messages as well as loading previous messages stored in MongoDB.

//server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
connections = [];

app.use(express.static(__dirname + '/public'));
server.listen(process.env.PORT || 4000);
console.log('Server Running');

//remaining code omitted for brevity

Answer №1

To ensure proper server binding, consider using the IP address 0.0.0.0 in your server.listen method like this: server.listen(process.env.PORT || 4000, '0.0.0.0'). Additionally, make sure to update the socket connection URL in your index.html file from 'http://127.0.0.1:4000' to match your internal IP address.

var socket = io.connect('http://192.168.1.1:4000');

This adjustment will help establish a more secure and efficient connection within your network.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The website is showing curly brackets coming from expressions utilizing Angular version 1.4

Being new to Angular, I anticipated encountering issues, but this particular problem seems to be recurring intermittently. While following a tutorial on learning Angular here, I was grasping the concept of using expressions. My goal is to access an gem&a ...

What are the drawbacks of relying on a dependent dependency in software development?

In a recent discussion on Stack Overflow, it was suggested that the best practice is to install packages from sub-dependencies for future use in the app, rather than directly from the dependencies node_modules folder. However, my scenario is a bit unique. ...

Determine the HTTP status code for a request using Vue.js and Ajax

I need to retrieve the HTTP status code after submitting a form (using the form submission function): return fetch(serviceUrl + 'Collect', { method: "POST", headers: new Headers({ "Content-Type": "application/json", Authoriza ...

Conditionally displaying ng-options in AngularJSI'm looking for a

After spending hours searching, I'm unable to find a solution to my problem. I was able to implement it before but lost the code and can't remember how I did it. I need to display only certain array values in a select box using ng-options. The d ...

Javascript: having trouble with closures and event listeners not getting added

There seems to be a recurring issue that I suspect is related to closures. I have 3 buttons, but when I execute this code, only the last button receives an event listener. This suspicion leads me to believe there might be a problem with closures. Despite ...

The iOS app icon for React Native is not appearing on the screen

After using XCode 12, I encountered an issue while trying to archive my iOS app. Despite no errors and no issues with app assets, the app icon mysteriously fails to show up. I've tried various solutions, including modifying codes in the pod file and r ...

How does the JSON data get served when Node.js is collaborating with CouchDB and Backbone.js?

Transitioning from a WordPress background to learning Node.js for a test app has been challenging. While Apache took care of most backend logic before, I'm now tasked with building it all myself. My main question revolves around serving JSON files fro ...

Extract the content from a <Span> element without specifying its ID

Is there a way to make it so that when a user clicks on an HTML tag, the text is copied to their clipboard? I also need to ensure that this functionality does not apply to a specific tag ID/name as I am unable to add those to my span. Is there a way to aut ...

Tips for incorporating a PDF file into a website when the Adobe Reader add-on is disabled in Internet Explorer

I attempted to insert a PDF into my website using the <embed> tag. Unfortunately, it doesn't seem to be functioning properly in Internet Explorer when the Adobe Reader add-on is disabled. Is there a solution that will allow it to work even if th ...

Utilizing the MongoDB Aggregation framework: implementing various $match stages to display distinct outcomes for a common value within a singular pipeline

Consider the following scenario with a collection of books: {BookDate: "BOOKA-2010", Price: "1", BookName: "BOOKA"}, {BookDate: "BOOKA-2011", Price: "2", BookName: "BOOKA"}, {BookDate: "BOOKA ...

Display a div based on particular input text across various strings

How can I enhance this Jquery code to display a button when the input value matches a certain string on keyup event? I have two questions regarding this: Fiddle $("#test").keyup(function () { $("#yeah").css("display", this.value == "test" ? "block" : ...

Utilizing session data within the passport module

In my current project, I am working on integrating various authentication methods for users using passport. I have already implemented passport-linkedin-oauth2, passport-google-oauth2, passport-facebook, and passport-twitter. While the specific method use ...

In my experience developing a weather application, I encountered issues with the state not updating and the application failing to re-render when I used axios.get() instead of fetch()

Currently, I am in the process of building a miniature weather app using React as a way to enhance my skills, particularly with Axios. This HTTP client is highly favored at the company where I am interning. However, I have encountered an issue that I would ...

The argument in question has not been defined

Experimenting with AngularJS, I encountered an error message in the browser console when trying to display a particular example: Error: Argument 'mainController as mainCtrl' is not a function, got undefined at Error (native) at bb My pr ...

Interact with AJAX form to dynamically display result data in the intended DIV

Seeking assistance from experienced JS users! Although most features are functional, including results being returned and forms submitting to MC database, I am encountering an issue where the result html is appearing in the incorrect DIV. Instead of displ ...

Interested in utilizing the Google Maps API on your local machine?

Recently, I encountered a problem with my Google Map API while trying to retrieve the nearest places. Here is the code snippet that caused the issue: var headers = { 'Access-Control-Allow-Origin' : '*', 'Content-Type&apo ...

A guide on incorporating `executeScript` in Rselenium for emulating the activation of hotkeys

While RSelenium is a useful package for interfacing with the web using R, it falls short when it comes to utilizing hotkeys to run external functions or activate extensions programmatically. One idea I'm exploring is using a javascript command to sim ...

How to fetch a specific element from an array using Node.js and M

Kindly incorporate this into my schema. newSchema({ 'product':{}, 'productPayment':[{}] }); The productPayment array consists of multiple elements, each with a unique ID. I am looking to retrieve only the array element that matc ...

The Discord.js guildMemberUpdate event: Everything you need to know

I am currently working on creating a welcome message bot using Discord.js. The goal is to have the bot send a welcome message through a webhook in a specific channel when a member gains access to it. Here's what I have so far: client.on("guildMe ...

*ngFor is not rendering the array data and no error is being shown

Currently utilizing mongoDB's $filter aggregation feature, which has successfully generated the expected output from my query. However, I am encountering an issue with my HTML code as *ngFor is not displaying the data and no errors are being shown in ...