Node - Broadcasting to specific clients with WebSockets/ws - javascript

I am developing an application where I would like to set up a system with a web socket system that supports namespacing. I'm using the WebSockets/ws websocket library and I'd like to take a socket.io inspired approach where clients can subscribe to topics and only receive messages sent for that topic but I cannot figure out how to implement this without using socket.io (I'd rather not use it) and I'm currently looking for any guidance that anyone may have on the subject.

Related

Is there anyway to create Laravel Websocket Server without 3rd party solution?

I am preparing to build a WebSockets system to have a bidirectional communication from my server to my clients through WebSockets.
I know that Laravel now very well supports 3rd parties like Pusher Channels and Ably.
There are also some other ways that are also very convenient and simple to use such as Laravel Websockets, soketi, laravel-echo-server.
But my problem is client-side cannot include 3rd party Client Library. My client-side is a Cross-platform Game based on Javascript and HTML5. I can only connect client-side to server-side through WebSocket.
As far as I know, when using 3rd party for server-side, I should use their JS Client Library like pusher/pusher-js, laravel/echo ... Therefore, I can't find any documents if I want to use them with WebSocket.
My goal is to find the best and most suitable solution for both client and server side.
What I have tried?
IDEA 1: Convert pusher.js to my own js
I spent 2 weeks on this. I see this as possible and some functions might work. However, it will take a long time if I want to convert the whole thing.
IDEA 2: Using Pusher and connect the client via Pusher Channels Protocol
I have tested them as follows
var socket = new WebSocket('ws://ws-[cluster_name].pusher.com:[port]/app/[key]');
It worked. I can see them connected on the Pusher Debug Console. However, I can't find any documentation regarding the basics like Subcribe channel, Listening events, Send message to other client, Ping, Pong, ...
IDEA 3: Using Laravel Websockets
Like idea 2, I couldn't find any documentation or examples.
IDEA 4: Using NodeJS instead of Laravel. Choose ws packages
I tried it and it worked as expected. WS is really simple but it is exactly what I want. However I don't have many years of NodeJS experience. Therefore, I feel there are many risks if I choose it to build a new project.
Could you tell me a best way to deal with this problem?
Thanks
The Pusher and Laravel Echo JavaScript clients work with any Pusher-compatible websockets host. (Pusher uses websockets, too.)
All it takes is a little configuration; see, for example, Laravel Websockets' guide on configuring Laravel and then configuring Echo. Both steps point your Pusher requests at your chosen Pusher replacement instead.
No need to write your own library or manually consume websocket data.

Realtime web programming: how does it work?

As a web developer, I have developed a chat service and some other real-time collaborative services with the help of third-party services like Redis or Pusher. They provide simple API's that I can use publish/subscribe model to achieve bidirectional communication over the webserver. I want to now implement a simple push notification without the use of any third-party services, but I am not quite sure how to achieve this. The situation is as below:
Backend is in Python (Django)
A user receives a private message from another user.
The recipient should be notified without having to refresh the web browser.
My questions:
From this post, Django seems a bad option to achieve such functionality while Node.js is a good one. Is it true? If it is, why is that?
Is it possible to open a websockets from client to the server, to listen to certain changes to a specific model? (ex. when there's a new message in Message model, update the DOM)
I appreciate any help a lot!
WebSockets are exactly what you want here. They are however, a (comparatively) recent addition to browsers and as a result, support is not ubiquitous. WebSocket Support
The websocket model allows you to connect a socket from a server to a client with a web browser, then send messages from the server and receive them asynchronously at the client and vice versa.
Because Node.js is Javascript and the chances are you will be writing your client in Javascript, it lends itself as a sensible choice for writing coupled components, such as in this scenario.
The most popular WebSocket library is Socket.io which was built for use with Node.js. With Socket.io, your notification model would look something like this:
Server
io.sockets.on('connection', function (socket) {
socket.emit('notification', { name: 'Notification', message: 'It\'s here');
});
Client
var notifications = document.getElementById('nlist');
socket.on('notification', function(notification) {
var div;
// make some DOM changes
document.title = notification.name;
div = document.createElement('div');
div.innerHTML = notification.message;
notifications.appendChild(div);
socket.emit('received-notification');
});
Socket.io allows you to use custom event names, as shown here, which makes designing communication APIs that little bit easier. It also has fallbacks for XHR Long Polling and Flash sockets, in the circumstance that the user does not have Web Sockets.
Node will be faster than Django in this circumstance, but you may find that your codebase is more manageable in Django if this is your first venture into Node. It can be kinda difficult to design an application with a series of callbacks like this. I haven't ever used WebSockets with Django, but my experience with Tornado and WebSockets has been poor.
If you just need unidirectional communication as pushing notifications, you'll better have a look at Server Side Events instead of Websockets.
SSE is much easier to implement, it doesn't need any special protocol (just standard HTTP) and does automatic reconnection.
Creating private messages would be done using good old XMLHttpRequests and should be filtered on client-slide.
Node.js is pretty good for serving SSE: its event loop implementation only uses one thread to manage multiple persistent connections.
Have a look at sse-pubsub which provides an easy way to send notifications.

Socket reading and writing from a web browser app

There is a server I need to talk to that publishes a protocol over TCP/IP for querying data from a database and listening on a socket to receive notifications when data is updated. The sever guys provide a Java API which uses this TCP protocol. This means I could easily write a Swing App to talk to this server.
I would like a browser based solution. As the protocol is known to me, could I do this in JavaScript? My app will have to display the data in a table. I have heard of Web Sockets but I'm not sure if it will allow this two way communication. Is it feasible? Is there a better way that is cross platform and will work in most browsers? Should I be considering a Java Swing based solution that runs inside a browser?
EDIT: What about changing the code in my C++ server to add an additional interface that my Javascript code can communicate directly with it?
The WebSocket protocol differs from TCP/IP sockets. You will have to write something to link them together.
You can do this perfectly well in JavaScript: use Node.js. There's enough tutorials to be found on the subject. The best way to link it to your in-browser JS is through Socket.IO.
Create a Node.js server that connects to the api
Make the server talk to your web app
Use it :)
This will work cross-platform and cross-browser (Socket.IO can use/emulate websockets even on IE6(!!)). You'll have to run a server-app (the Node.js app) though.
My personal opinion is that if you want a web/browser based solution, you should use native technology, and not Java.
Hope this helps :)

ASP.NET MVC - Automatic notifications when a record is modified?

I have just used the NotifyIcon class in a windows application and I think it is really handy. I'm predominantly a web developer so I just wanted to find out if there is anything similar to this for a website.
The website I want to incorporate this into has a Ticket Management module where users can capture tickets/problems and then get responses to these tickets from my client's employees who handle the ticket.
Obviously I realize that the notification or pop up will need to be shown in the page, but is there a way to put a timer on the specific page, or even the Master page (maybe javascript or JQuery), to poll the database every few minutes and check for recently modified tickets and let the logged in user know that a ticket has been updated?
Thanks in advance.
If you need the client to keep an open connection to the server and poll it, I think Signal R will be your best bet for integrating into an .NET project. It is on Nuget but source is at https://github.com/SignalR/SignalR.
I would recommend familiarizing yourself with Node.js.
Node.js is a strong tool that aids in leveraging javascript as a real-time server management tool.
After you've gotten yourself familiarized with the Node.js setup, you'll want to grab Socket.io. Socket.io provides suppport for the long-polling technique by leveraging against your websocket created by Node.js. Here, we can manage the conditions at which we serve data. This is a huge tool on the developers side in battling against 2 HTTPD ports.

Instant Message on browser

Introduction: I want to develop the chat client that user can chat on the browser and I use the protocol call xmpp. Because of HTML5 web socket not yet available I try flash xmlsocket instead.
Problem: I cannot connect to the server via browser. I'm not sure why, but I think that it is the problem of the server configuration.
Question: Which is the best jabber server suitable for this job?
Most probably this is a permission issue. Either that or you've just configured the wrong host/port to connect to. Flash is not allowed to connect to other hosts than the one it was gotten from itself. You have to explicitely allow flash connections on the receiving side of the request (so on the chat server that you are connecting to). Google for crossdomain.xml to get more info.
ejabberd sounds like a suitable option for you. ejabberd is xmpp server written in erlang and is used quite widely. Many of the well known web based im services like www.meebo.com etc are known to be running on ejabberd. It allows you to install other transports which would let you enable talking to users of other protocol like yahoo, msn, icq etc.
You can also have a look at the xiff action script library by ignite realtime. It is an xmpp client library in action script. If you use this library you would just need to implement the ui components.
Hum... flash is ok, but you'd be better of using something like BOSH, which is basically an HTTP layer over XMPP.
Ejabberd would be a good server, as it supports BOSH, I don't know about OpenFire or Tigase (but I'd say they do). Other servers should be looked at carefully because they don't seem to have a "dynamic" community.
You will need a lot of Javascript, and for that, I can recommend StropheJS, which is probably the very best library out there today.
We have created a MUC (Multi-user chat room) client called Aristochat that works in the browser recently at Superfeedr. You can find the code on Github and an example here.

Categories