Unable to establish a websocket connection with either Amber or NPM, uncertain of the reason

Amber CLI (amberframework.org) - v0.11.3 Crystal 0.27.0 [c9d1eef8f] (2018-11-01) LLVM: 4.0.0 Default target: x86_64-unknown-linux-gnu npm 3.5.2

Attempting to incorporate sockets using Crystal Lang and Amber has hit a snag. Despite following the guidelines outlined in the Amber Docs as well as the instructions on Github Amber Readme, an error is persisting.

The process involves running "npm run watch" and "amber watch," which successfully triggers a browser refresh whenever changes are made to main.js.

//main.js
import 'bootstrap';
import $ from 'jquery';
import Amber from 'amber';

let socket = new Amber.Socket('/notification')
socket.connect().then(function() {
  let channel = socket.channel('chat_room:hello');

  channel.join();

  channel.push('message_new', {
    message: "Hello Amber from WebSocket Client!"
  });

  channel.on('message_new', function(payload) {
    console.log(payload);
  });
});
// Uncaught TypeError: Cannot read property 'Socket' of undefined
// at Module../src/assets/javascripts/main.js (main.js:28)
// at __webpack_require__ (bootstrap:19)
// at Module../config/webpack/entry.js (entry.js:1)
// at __webpack_require__ (bootstrap:19)
// at bootstrap:83
// at bootstrap:83

UPDATE:

Closer inspection revealed the existence of two versions of main.js. The version triggering the error resides in "webpack://.src/assets/javascripts/main.js." Examination during run-time showed that Amber could not be found, leading to a "ReferenceError: amber is not defined." On the other hand, the second version of the file is situated in "/dist/main.bundle.js." Puzzlingly, both assets are being served simultaneously. Could this issue potentially stem from npm?

Answer №1

I was fortunate to locate a resolution on the Amber Forums. Evidently, a mistake was made in the default functionality during a commit, and a pull request has been submitted. Below is jackturnbull's response which I have reproduced.

For Solution 1, modify the Amber import line to:

import * as Amber from 'amber';

Regarding Solution 2:

import { Channel, Socket } from 'amber';

// Then reference as:
let socket = new Socket('/notification');
...

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

Using Rails to Pass Front-End JavaScript Data from an External API to the Controller

I need to retrieve coordinates from an external API, specifically the Google Maps API, and then send it to my controller. However, I am encountering a 500 internal server error when using jQuery/Ajax for this task. Researching the issue online suggests tha ...

Having issues with starting npm, can anyone assist me?

I encountered a problem today and tried using commands like npm install, but I couldn't find a solution. Can anyone help me out? The issue started when I pressed ctrl-c and then used npm start to reload the server, and that's when all these error ...

What is the purpose of the video-js endcard plugin incorporating native controls into the player?

Endcard is a unique plugin designed for the video-js html5 video player that adds clickable links to the end of a video. (More information on endcards can be found here: https://github.com/theonion/videojs-endcard). To implement an endcard, simply include ...

How can I replicate the functionality of the span element using Javascript?

Using Javascript, I am able to display a paragraph without the need for HTML. By adding it to an HTML id, I can manipulate individual words within the text. My goal is to make specific words cursive while keeping the entire paragraph in its original font s ...

use element ui tree and vue to filter files according to selected folder

Utilizing the element UI treeview to showcase folders. Each folder or its child folder contains files that need to be displayed based on folder selection. While it's easy to filter and list out these files in a normal list, I am facing challenges with ...

What is the best way to free up memory after receiving responseText in a continuous streaming request?

Utilizing xmlHTTPRequest to fetch data from a continuous motion JPEG data stream involves an interesting trick where responseText can populate data even before the request is completed, since it will never actually finish. However, I have encountered some ...

What is the best approach for overlaying random meshes onto a terrain using a heightmap in three.js?

I'm interested in plotting randomly generated meshes at the y-position that corresponds to the terrain's height in three.js. While browsing through the documentation, I came across the raycaster method, which seems like it could be useful. Howeve ...

Using Vue.js to loop through data objects when a button is clicked

I'm currently working on building a quiz functionality using vue.js, but I've hit a roadblock in trying to make the "Next" button cycle through my data. The goal is to have the screen display the object containing the question and answers (e.g. q ...

Retrieve the number of rows in the table component

I'm in need of getting the row count from a table component that I have built using vuejs. My goal is to display the row count outside of the table component structure. The issue with the code snippet below is that it doesn't show the correct row ...

Is it necessary for each React component to have its own individual stylesheet?

Looking for some advice on React as a newbie here. I'm wondering whether each React component should have its own stylesheet. For instance, if I have my main App component that gets rendered to the browser, is it sufficient to include a CSS file the ...

Trouble displaying REACT.jsx in browser

I'm currently learning React and struggling to get it running smoothly. HTML function Person(){ return ( <div class="person"> <h1>Max</h1> <p>Your Age: 28</p> </div> ); } ...

The nodejs events function is being triggered repeatedly

I have been developing a CMS on nodejs which can be found at this link. I have incorporated some event hooks, such as: mpObj.emit('MP:FOOTER', '<center>MPTEST Plugin loaded successfully.</center>'); Whenever I handle this ...

Ensure to use e.preventDefault() method when handling form submissions

Check out this code snippet: <form> <input type="text" name="keyword" value="keyword"> <input type="submit" value="Search"> </form> I'm seeking assistance with implementing jQuery to prevent the default action of the submit b ...

Wait for a reply from one GET request before initiating the next one in node

When working with node, I am making two consecutive calls to an API. My goal is to ensure that the first GET request has completed before triggering the second one, using data from the response of the first call. To achieve this, I have experimented with ...

Is it possible to update the URL dynamically in a Next.js application without relying on the <Link> component?

I'm looking for a way to dynamically change the URL on a page without using <Link>. Specifically, I want to switch to a different URL when a variable changes from false to true, regardless of user interaction. Currently, I'm achieving this ...

Submitting a file to the Slack API via the files.upload method using jQuery

I'm attempting to upload a file on a webpage and send it to Slack using the Slack API. Initially, my code looked like this: var request = require('request'); $(".submit").click(function(){ request.post({ url: 'https://slack.co ...

Utilizing AJAX for XML data parsing

I need help with formatting XML data into a table. The code I've written isn't working as expected. The XML data is structured in branches, causing it to not display correctly. Can someone assist me in fixing this issue? <!DOCTYPE html> &l ...

Unexpected bug encountered while implementing redux

I received a warning from eslint while working with create-react-app. ./src/components/auth.js Line 24: Unexpected labeled statement no-labels Line 24: 'authenticated:' is defined but never used ...

Echo a JS variable into PHP and then insert a PHP file into an HTML element - a step-by-step guide!

Greetings to the wonderful community at stackoverflow, I am currently working on a variable code that allows for easy insertion of code from another file, such as a div. I am curious if it is possible to include a PHP file using JavaScript and JS variable ...

The Jquery.post() function failed to trigger the callback

I'm encountering an issue with the JQUERY Post function. There are 2 functions that utilize the JQUERY Post function. Both of them work fine, but the callback function (handleLike) is not being triggered. Interestingly, when I manually call handleLi ...