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
?
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.
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.
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.
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.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With