1

I'm new to angularjs and can use some guidance on the design of an app which uses a WebSocket connection to a backend. When the socket gets an onmessage event, it should load a dynamic view. Prior to loading the view, the model needs to be constructed based on some of the message parameters.

So, is it an acceptable angular solution to call a controller function from the onmessage event handler or is there a more angular way of doing things? If it is acceptable, how can I accomplish this? Should I call a route and pass in the route parameters to the controller? Thanks.

1
  • 1
    In the case that the backend sends data via the websocket independently of the user initiating anything, I would use $broadcast to $broadcast whatever your backend whats to push to the app. Then you can pick up this event from anywhere within your application. Commented Jul 22, 2014 at 20:16

2 Answers 2

1

If onmessage event is a websocket server response to client request, you can use promise/deferred implementation using angular service $q. Here is a good explanation: http://clintberry.com/2013/angular-js-websocket-service/

If onmessage event is push data from websocket server, you can use $broadcast to broadcast onmessage event and $on in controller to listen the broadcast event.

I have code that may help you. It is angular service for xively.com REST API using websocket: https://github.com/mgalela/ngxively

Sign up to request clarification or add additional context in comments.

Comments

0

You will not call a controller. You will just change the route to the view and run the initialization logic there.

This could be done in few ways. If the data that you need to initialize the view is small you could have it as a route parameter (aka state parameter).

The URL will look something like this:

http://yoursite.org/theView/theParameter

If the data large you could have a service that both controllers will inject to share data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.