Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we import stompjs in a browser? Or something like stompjs?

Tags:

stompjs

I am trying to follow this Spring tutorial on how to use websockets. I am using webpack to bundle my code and babel to convert it from ES6. I am trying to pull in sockjs with a normal import statement.

import SockJS from 'sockjs'

But when webpack runs, I get missing module errors,

ERROR in ./~/stompjs/lib/stomp-node.js
Module not found: Error: Cannot resolve module 'net' in /Users/name/Developer/cubs-stack-4/cubs-webapp/node_modules/stompjs/lib
 @ ./~/stompjs/lib/stomp-node.js 14:8-22

ERROR in ./~/websocket/package.json
Module parse failed: /Users/name/Developer/cubs-stack-4/cubs-webapp/node_modules/websocket/package.json Line 2: Unexpected token :
You may need an appropriate loader to handle this file type.
| {
|   "_args": [
|     [
|       "websocket@latest",
 @ ./~/websocket/lib/version.js 1:17-43

mainly because it is expecting to be run on Node.

I have 2 questions.

First, how do I get stompjs into my browser side code using an import/require statement?

Second, how come in the tutorial, they can drop stompjs in the HEAD and it doesn't blow up in the browser, but it does when I run the "same" code through webpack?

like image 262
jhamm Avatar asked Mar 25 '16 22:03

jhamm


People also ask

How do you use Stompjs?

Create a STOMP client STOMP JavaScript clients will communicate to a STOMP server using a ws:// URL. To create a STOMP client JavaScript object, you need to call Stomp. client(url) with the URL corresponding to the server's WebSocket endpoint: var url = "ws://localhost:15674/ws"; var client = Stomp.

How do SockJS work?

SockJS is a library that mimics the native WebSockets API. Additionally, it will fall back to HTTP whenever a WebSocket fails to connect, or if the browser being used doesn't support WebSockets. Like WS, SockJS requires a server counterpart; its maintainers provide both a JavaScript client library and a Node.

What is SockJS node?

SockJS is a JavaScript library (for browsers) that provides a WebSocket-like object. SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server, with WebSockets or without.

Is STOMP deprecated?

This project is no longer maintained. If you encounter bugs with it or need enhancements, you can fork it and modify it as the project is under the Apache License 2.0.


1 Answers

installing 'net' dependency solved my issue

npm i sockjs-client --save
npm i stompjs --save
npm i net

and import like this

import * as SockJS from 'sockjs-client';
import * as Stomp from 'stompjs';
like image 185
Mashrabbek Akbarov Avatar answered Oct 21 '22 05:10

Mashrabbek Akbarov