GithubHelp home page GithubHelp logo

dewafer / websocket-demo Goto Github PK

View Code? Open in Web Editor NEW
33.0 7.0 15.0 531 KB

A demo APP implemented web-socket using Spring Boot, Vue, SockJs and Stomp.

HTML 4.01% JavaScript 52.10% Vue 20.70% Java 23.18%
spring-boot spring websocket vue sockjs stomp

websocket-demo's Introduction

WEBSOCKET-DEMO

Build Status

This is a prototype project demonstrated how to build a web socket project using Spring Boot and Vue.

In this project, there is a ticker service emitting message to the Vue client every one second. And in the Vue UI client, there is an input and button to send message back to the server and the server will log that message to the console.

How to build and run

Build with gradle:

> ./gradlew clean build

You can also run using spring-boot plugin:

> ./gradlew bootRun

This probject will build into a war file, you can deploy that war file into your app server, or you can also just run that war file using java -jar build/libs/websocket-demo-0.0.1-SNAPSHOT.war.

To build the Vue UI, make sure npm is installed on your system and

> cd src/main/frontend
> npm install && npm run build

Please refer to the UI's README file.

Then UI will be built into src/main/resources/static. The server will host the ui static files.

Note that you don't have to build the UI, it's pre-built and included in the source.

After all builds are successful, open http://localhost:8080 in your web browser.

How to debug Vue

If you want to debug the UI, you can cd into the frontend folder and just run the dev build like this:

> cd src/main/frontend
> npm run dev

Remember to run npm install beforehand.

What's to learn?

For Spring's WebSocket's support:

  • First, to enable websocket in Spring Boot, you should include spring-boot-starter-websocket in your dependency.
  • Second, config a message borker like this one WebSocketConfig.java.
  • The @EnableWebSocketMessageBroker is required, Spring's auto config won't auto-config that for you.
  • To receive message from client, use @MessageMapping annotation on controllers, like @RequestMapping does.
  • To send message to client, use @SendTo annotation on controllers, like @ResponseBody does.
  • Note that if you are sending message on threads other than controllers, @SendTo won't work (Not 100% sure, I'm sure it doesn't work on @Scheduled methods).
  • To send message from things other than controllers, SimpMessagingTemplate is required. Refer to TickService.java for more details.
  • Spring Websocket support is based on SockJS and Stomp.
  • Please refer to 26. WebSocket Support official document for more details. (It's a very long doc, please be patient.)
  • And there's a quick guide in Spring guides called Using WebSocket to build an interactive web application to let you get started very fast.

For Vue's:

  • I couldn't find a good SockJS and Stomp plugin for Vue, if you know one please let me know thanks.
  • So to communicate with Spring's websocket, sockjs-client and webstomp-client are used.
  • WebSocket is connected when the Home component is mounted, please refer to Home.vue for more details.
  • The code about SockJS and Stomp was refered from Create a browser client section from Spring's guide, but the origin one is using JQuery.

TODOs

  • Change WebSocket messages into json instead of string.
  • Refactor WebSocket in Home.vue into a service utilizing Promise or a Vue plugin (Don't know how to write Vue plugin).
  • Refactor into meanful demo like a web chat app or message notification app.
  • Integrate npm build process into gradle build.
  • Separate frontend and backend into 2 isolated projects???

websocket-demo's People

Contributors

dewafer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.