New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How not to use SockJS #14

Closed
Glamdring opened this Issue Nov 21, 2013 · 17 comments

Comments

Projects
None yet
8 participants
@Glamdring

Glamdring commented Nov 21, 2013

I don't need to use SockJS - I would just like to use a regular websocket. Stomp provides: Stomp.client("ws://:/endpoint"). However, that doesn't seem to work with this example. Can it work, and how to make it work like that.
Probably it would be nice ot have a page without SockJS.

@mikesir87

This comment has been minimized.

mikesir87 commented Nov 21, 2013

Just wondering... why would you even want to do this? SockJS is only an abstraction layer that lets you create a persistent connection to the server without having to care about the actual transport protocol being used.

If you don't use SockJS, you'll then need to write your own logic (both server and client) to determine if websockets is supported and if not, what fallbacks are available. Having had to do that before, it's no fun. I'd be interested to find out why you would want to forgo using SockJS.

@Glamdring

This comment has been minimized.

Glamdring commented Nov 22, 2013

Right now - because I want to experiment with all options - no sockjs, no stompjs, spring, javax.websocket, etc.

For a regular application you are right. My only concern is that it is yet-another-dependency, but that's rarely an issue, I agree.

@rstoyanchev

This comment has been minimized.

Owner

rstoyanchev commented Nov 22, 2013

Have you tried turning off SockJS support in the configuration? i.e. removing .withSockJS().

BTW you can also connect to a raw WebSocket (i.e. no SockJS frames) even with SockJS (i.e "/portfolio/websocket"). See the section on "Connecting to SockJS without the client" in the client page.

Also for what it's worth on the server-side at least using SockJS doesn't require any new dependencies.

@rstoyanchev

This comment has been minimized.

Owner

rstoyanchev commented Dec 16, 2013

To summarize, to remove the SockJS layer, remove .withSockJS() in WebSocketConfig.java and use new WebSocket('ws://localhost:8080/spring-websocket-portfolio/portfolio') on the client side.

@rstoyanchev

This comment has been minimized.

Owner

rstoyanchev commented Sep 7, 2014

It sounds very unusual. Is it possible to create a simple example some place that demonstrates the issue?

@francoisledroff

This comment has been minimized.

francoisledroff commented Oct 14, 2014

All I needed was remove .withSockJs in the websocket config on the server side.
Then on the client, remove the import of sokjs js library
and use stomp nevertheless, replace

    var socket = new SockJS('/spring-websocket-portfolio/portfolio');
    var stompClient = Stomp.over(socket);

by

     var url = "ws://localhost:8080//spring-websocket-portfolio/portfolio?access_token="+authToken;
     var stompClient = Stomp.client(url);

why, would I do that?

wellI have 2 reasons:

  • I don't need to support all funky/out-dated browsers out there
  • I want to pass an auth token, and with sockJs it's not possible cf. sockjs/sockjs-client#196
@cooniur

This comment has been minimized.

cooniur commented Jan 5, 2016

In Spring 4.2, just use the StandardWebSocketClient, and handle the message by implementing WebSocketHandler interface.

Something like:

def client = new StandardWebSocketClient()
client.doHandshake(new MyWebSocketHandler(), rtmWsUrl)

private static class MyWebSocketHandler implements WebSocketHandler {
    ....
}
@rstoyanchev

This comment has been minimized.

Owner

rstoyanchev commented Jan 5, 2016

Indeed, and the chapter in the reference documentation is organized starting with a sub-section on WebSocket followed by SockJS and then STOMP.

@yovinn

This comment has been minimized.

yovinn commented Jan 20, 2017

7455

@yovinn

This comment has been minimized.

yovinn commented Jan 20, 2017

i can not understand your mean!

@thoslin

This comment has been minimized.

thoslin commented Apr 1, 2017

I had the same issue with OP. The scenario is that I tried to connect to spring websocket from iOS, however I didn't find any usable Stomp library that supports sockjs. So I tried to use Stomp without sockjs. Per the documentation https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-intro.

Or if connecting via WebSocket (without SockJS):

var socket = new WebSocket("/spring-websocket-portfolio/portfolio");
var stompClient = Stomp.over(socket);

stompClient.connect({}, function(frame) {
}

I removed the with-sockjs line from my configuration and tested the above code in chrome dev tools. However it didn't work. The error is URL invalid. Then I used the URL "ws://domain/spring-websocket-portfolio/portfolio", it then gave me 400 Bad Request.

So without sockjs, which url should I connect to?

@acrive82

This comment has been minimized.

acrive82 commented Apr 5, 2017

Removing .withSockJS()

Caused by: java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found

@rstoyanchev

This comment has been minimized.

Owner

rstoyanchev commented Apr 5, 2017

So without sockjs, which url should I connect to?

The same.

No suitable default RequestUpgradeStrategy found

Old server version perhaps?

@acrive82

This comment has been minimized.

acrive82 commented Apr 6, 2017

I use SpringBoot 1.5.2 with tomcat starter. I'm try to figure out why this happens : link

@rstoyanchev

This comment has been minimized.

Owner

rstoyanchev commented Apr 6, 2017

That links shows a different error than the "RequestUpgradStrategy not found".

@thoslin

This comment has been minimized.

thoslin commented Apr 10, 2017

@rstoyanchev it's not working for me. I tried:

var ws = new WebSocket("ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio")

It gave the error:

WebSocket connection to 'ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio' failed: Error during WebSocket handshake: Unexpected response code: 400

while if I tried

var ws = new WebSocket("ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio/websocket")

It then gave the error

WebSocket connection to 'ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio/websocket' failed: Error during WebSocket handshake: Unexpected response code: 404

@rstoyanchev

This comment has been minimized.

Owner

rstoyanchev commented Apr 10, 2017

If SockJS is turned off there is no point in trying /{endpoint}/websocket since that's a URL structure only supported with SockJS.

Well 400 means bad input. Look further. Response headers, DEBUG logging for org.springframework.web, step through with debugger if needed. You're missing something basic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment